{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PSbbmDn4fHY3",
        "outputId": "b2782ffb-fb71-4391-8895-a25df9c2f211"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Collecting optuna\n",
            "  Using cached optuna-4.2.1-py3-none-any.whl.metadata (17 kB)\n",
            "Collecting alembic>=1.5.0 (from optuna)\n",
            "  Using cached alembic-1.15.1-py3-none-any.whl.metadata (7.2 kB)\n",
            "Collecting colorlog (from optuna)\n",
            "  Using cached colorlog-6.9.0-py3-none-any.whl.metadata (10 kB)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from optuna) (2.0.2)\n",
            "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.11/dist-packages (from optuna) (24.2)\n",
            "Requirement already satisfied: sqlalchemy>=1.4.2 in /usr/local/lib/python3.11/dist-packages (from optuna) (2.0.39)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from optuna) (4.67.1)\n",
            "Requirement already satisfied: PyYAML in /usr/local/lib/python3.11/dist-packages (from optuna) (6.0.2)\n",
            "Requirement already satisfied: Mako in /usr/lib/python3/dist-packages (from alembic>=1.5.0->optuna) (1.1.3)\n",
            "Requirement already satisfied: typing-extensions>=4.12 in /usr/local/lib/python3.11/dist-packages (from alembic>=1.5.0->optuna) (4.12.2)\n",
            "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.11/dist-packages (from sqlalchemy>=1.4.2->optuna) (3.1.1)\n",
            "Using cached optuna-4.2.1-py3-none-any.whl (383 kB)\n",
            "Using cached alembic-1.15.1-py3-none-any.whl (231 kB)\n",
            "Using cached colorlog-6.9.0-py3-none-any.whl (11 kB)\n",
            "Installing collected packages: colorlog, alembic, optuna\n",
            "Successfully installed alembic-1.15.1 colorlog-6.9.0 optuna-4.2.1\n",
            "\u001b[31mERROR: Could not find a version that satisfies the requirement hyperpot (from versions: none)\u001b[0m\u001b[31m\n",
            "\u001b[0m\u001b[31mERROR: No matching distribution found for hyperpot\u001b[0m\u001b[31m\n",
            "\u001b[0mRequirement already satisfied: xgboost in /usr/local/lib/python3.11/dist-packages (2.1.4)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from xgboost) (2.0.2)\n",
            "Requirement already satisfied: nvidia-nccl-cu12 in /usr/local/lib/python3.11/dist-packages (from xgboost) (2.21.5)\n",
            "Requirement already satisfied: scipy in /usr/local/lib/python3.11/dist-packages (from xgboost) (1.14.1)\n"
          ]
        }
      ],
      "source": [
        "!pip install optuna\n",
        "!pip install hyperpot\n",
        "!pip install xgboost"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ps5oyaU4fVBp"
      },
      "outputs": [],
      "source": [
        "import zipfile\n",
        "from google.colab import drive\n",
        "import pandas as pd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "E_MHIFp-hJYH",
        "outputId": "f821c84d-51d7-4517-f6b6-6596750f0ef8"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Downloading...\n",
            "From: https://drive.google.com/uc?id=108Rg2FdrnQuh2O_klrQWalZI7orvN5fj\n",
            "To: /content/Diagnostics.xlsx\n",
            "\r  0% 0.00/832k [00:00<?, ?B/s]\r100% 832k/832k [00:00<00:00, 25.4MB/s]\n"
          ]
        }
      ],
      "source": [
        "!gdown 108Rg2FdrnQuh2O_klrQWalZI7orvN5fj"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Ik-osSQuydZ2"
      },
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZwTaJ_aAhO1p"
      },
      "outputs": [],
      "source": [
        "df = pd.read_excel(\"/content/Diagnostics.xlsx\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 261
        },
        "id": "tA-qoRgWhbnZ",
        "outputId": "5cdfd8c1-8f05-4d73-be4a-fae7a928f81d"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 10646,\n  \"fields\": [\n    {\n      \"column\": \"FileName\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 10646,\n        \"samples\": [\n          \"MUSE_20180113_120154_01000\",\n          \"MUSE_20180712_154546_13000\",\n          \"MUSE_20180115_120656_35000\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Rhythm\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 11,\n        \"samples\": [\n          \"ST\",\n          \"AFIB\",\n          \"SAAWR\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Beat\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 742,\n        \"samples\": [\n          \"ALS AQW TWC\",\n          \"ARS CCR TWC\",\n          \"APB TWC VPB\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"PatientAge\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 18,\n        \"min\": 4,\n        \"max\": 98,\n        \"num_unique_values\": 95,\n        \"samples\": [\n          7,\n          18,\n          82\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Gender\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"FEMALE\",\n          \"MALE\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"VentricularRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 34,\n        \"min\": 34,\n        \"max\": 263,\n        \"num_unique_values\": 189,\n        \"samples\": [\n          195,\n          211\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"AtrialRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 60,\n        \"min\": 0,\n        \"max\": 535,\n        \"num_unique_values\": 253,\n        \"samples\": [\n          284,\n          86\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSDuration\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 17,\n        \"min\": 18,\n        \"max\": 256,\n        \"num_unique_values\": 80,\n        \"samples\": [\n          122,\n          114\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTInterval\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 59,\n        \"min\": 114,\n        \"max\": 736,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          440,\n          420\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTCorrected\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 39,\n        \"min\": 219,\n        \"max\": 760,\n        \"num_unique_values\": 295,\n        \"samples\": [\n          332,\n          531\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"RAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 41,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 277,\n        \"samples\": [\n          42,\n          -10\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 57,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 356,\n        \"samples\": [\n          101,\n          36\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSCount\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 5,\n        \"min\": 5,\n        \"max\": 40,\n        \"num_unique_values\": 35,\n        \"samples\": [\n          36,\n          25\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOnset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 6,\n        \"min\": 159,\n        \"max\": 240,\n        \"num_unique_values\": 62,\n        \"samples\": [\n          171,\n          170\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 8,\n        \"min\": 249,\n        \"max\": 331,\n        \"num_unique_values\": 68,\n        \"samples\": [\n          283,\n          264\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 29,\n        \"min\": 281,\n        \"max\": 582,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          421,\n          422\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
              "type": "dataframe",
              "variable_name": "df"
            },
            "text/html": [
              "\n",
              "  <div id=\"df-2b07e70d-c511-491b-9f6e-31de4ee5c1a4\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>FileName</th>\n",
              "      <th>Rhythm</th>\n",
              "      <th>Beat</th>\n",
              "      <th>PatientAge</th>\n",
              "      <th>Gender</th>\n",
              "      <th>VentricularRate</th>\n",
              "      <th>AtrialRate</th>\n",
              "      <th>QRSDuration</th>\n",
              "      <th>QTInterval</th>\n",
              "      <th>QTCorrected</th>\n",
              "      <th>RAxis</th>\n",
              "      <th>TAxis</th>\n",
              "      <th>QRSCount</th>\n",
              "      <th>QOnset</th>\n",
              "      <th>QOffset</th>\n",
              "      <th>TOffset</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>MUSE_20180113_171327_27000</td>\n",
              "      <td>AFIB</td>\n",
              "      <td>RBBB TWC</td>\n",
              "      <td>85</td>\n",
              "      <td>MALE</td>\n",
              "      <td>117</td>\n",
              "      <td>234</td>\n",
              "      <td>114</td>\n",
              "      <td>356</td>\n",
              "      <td>496</td>\n",
              "      <td>81</td>\n",
              "      <td>-27</td>\n",
              "      <td>19</td>\n",
              "      <td>208</td>\n",
              "      <td>265</td>\n",
              "      <td>386</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>MUSE_20180112_073319_29000</td>\n",
              "      <td>SB</td>\n",
              "      <td>TWC</td>\n",
              "      <td>59</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>52</td>\n",
              "      <td>52</td>\n",
              "      <td>92</td>\n",
              "      <td>432</td>\n",
              "      <td>401</td>\n",
              "      <td>76</td>\n",
              "      <td>42</td>\n",
              "      <td>8</td>\n",
              "      <td>215</td>\n",
              "      <td>261</td>\n",
              "      <td>431</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>MUSE_20180111_165520_97000</td>\n",
              "      <td>SA</td>\n",
              "      <td>NONE</td>\n",
              "      <td>20</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>67</td>\n",
              "      <td>67</td>\n",
              "      <td>82</td>\n",
              "      <td>382</td>\n",
              "      <td>403</td>\n",
              "      <td>88</td>\n",
              "      <td>20</td>\n",
              "      <td>11</td>\n",
              "      <td>224</td>\n",
              "      <td>265</td>\n",
              "      <td>415</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>MUSE_20180113_121940_44000</td>\n",
              "      <td>SB</td>\n",
              "      <td>NONE</td>\n",
              "      <td>66</td>\n",
              "      <td>MALE</td>\n",
              "      <td>53</td>\n",
              "      <td>53</td>\n",
              "      <td>96</td>\n",
              "      <td>456</td>\n",
              "      <td>427</td>\n",
              "      <td>34</td>\n",
              "      <td>3</td>\n",
              "      <td>9</td>\n",
              "      <td>219</td>\n",
              "      <td>267</td>\n",
              "      <td>447</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>MUSE_20180112_122850_57000</td>\n",
              "      <td>AF</td>\n",
              "      <td>STDD STTC</td>\n",
              "      <td>73</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>162</td>\n",
              "      <td>162</td>\n",
              "      <td>114</td>\n",
              "      <td>252</td>\n",
              "      <td>413</td>\n",
              "      <td>68</td>\n",
              "      <td>-40</td>\n",
              "      <td>26</td>\n",
              "      <td>228</td>\n",
              "      <td>285</td>\n",
              "      <td>354</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-2b07e70d-c511-491b-9f6e-31de4ee5c1a4')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-2b07e70d-c511-491b-9f6e-31de4ee5c1a4 button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-2b07e70d-c511-491b-9f6e-31de4ee5c1a4');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-f1755a5c-0a80-4b1a-9e8e-28b1a685fb8f\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-f1755a5c-0a80-4b1a-9e8e-28b1a685fb8f')\"\n",
              "            title=\"Suggest charts\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "      --bg-color: #E8F0FE;\n",
              "      --fill-color: #1967D2;\n",
              "      --hover-bg-color: #E2EBFA;\n",
              "      --hover-fill-color: #174EA6;\n",
              "      --disabled-fill-color: #AAA;\n",
              "      --disabled-bg-color: #DDD;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "      --bg-color: #3B4455;\n",
              "      --fill-color: #D2E3FC;\n",
              "      --hover-bg-color: #434B5C;\n",
              "      --hover-fill-color: #FFFFFF;\n",
              "      --disabled-bg-color: #3B4455;\n",
              "      --disabled-fill-color: #666;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart {\n",
              "    background-color: var(--bg-color);\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: var(--fill-color);\n",
              "    height: 32px;\n",
              "    padding: 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: var(--hover-bg-color);\n",
              "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: var(--button-hover-fill-color);\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart-complete:disabled,\n",
              "  .colab-df-quickchart-complete:disabled:hover {\n",
              "    background-color: var(--disabled-bg-color);\n",
              "    fill: var(--disabled-fill-color);\n",
              "    box-shadow: none;\n",
              "  }\n",
              "\n",
              "  .colab-df-spinner {\n",
              "    border: 2px solid var(--fill-color);\n",
              "    border-color: transparent;\n",
              "    border-bottom-color: var(--fill-color);\n",
              "    animation:\n",
              "      spin 1s steps(1) infinite;\n",
              "  }\n",
              "\n",
              "  @keyframes spin {\n",
              "    0% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "      border-left-color: var(--fill-color);\n",
              "    }\n",
              "    20% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    30% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    40% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    60% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    80% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "    90% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const quickchartButtonEl =\n",
              "        document.querySelector('#' + key + ' button');\n",
              "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
              "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
              "      try {\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      } catch (error) {\n",
              "        console.error('Error during call to suggestCharts:', error);\n",
              "      }\n",
              "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
              "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-f1755a5c-0a80-4b1a-9e8e-28b1a685fb8f button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "                     FileName Rhythm       Beat  PatientAge  Gender  \\\n",
              "0  MUSE_20180113_171327_27000   AFIB   RBBB TWC          85    MALE   \n",
              "1  MUSE_20180112_073319_29000     SB        TWC          59  FEMALE   \n",
              "2  MUSE_20180111_165520_97000     SA       NONE          20  FEMALE   \n",
              "3  MUSE_20180113_121940_44000     SB       NONE          66    MALE   \n",
              "4  MUSE_20180112_122850_57000     AF  STDD STTC          73  FEMALE   \n",
              "\n",
              "   VentricularRate  AtrialRate  QRSDuration  QTInterval  QTCorrected  RAxis  \\\n",
              "0              117         234          114         356          496     81   \n",
              "1               52          52           92         432          401     76   \n",
              "2               67          67           82         382          403     88   \n",
              "3               53          53           96         456          427     34   \n",
              "4              162         162          114         252          413     68   \n",
              "\n",
              "   TAxis  QRSCount  QOnset  QOffset  TOffset  \n",
              "0    -27        19     208      265      386  \n",
              "1     42         8     215      261      431  \n",
              "2     20        11     224      265      415  \n",
              "3      3         9     219      267      447  \n",
              "4    -40        26     228      285      354  "
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "df.head()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "i-XYhrNBychW",
        "outputId": "cc9bea08-46d8-4d70-9dc2-5b5c980b4474"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Rhythm\n",
            "AF        445\n",
            "AFIB     1780\n",
            "AT        121\n",
            "AVNRT      16\n",
            "AVRT        8\n",
            "SA        399\n",
            "SAAWR       7\n",
            "SB       3889\n",
            "SR       1826\n",
            "ST       1568\n",
            "SVT       587\n",
            "Name: count, dtype: int64\n"
          ]
        }
      ],
      "source": [
        "import pandas as pd\n",
        "\n",
        "\n",
        "\n",
        "# Conta i valori unici nella colonna\n",
        "valori_unici = df[\"Rhythm\"].value_counts().sort_index()\n",
        "\n",
        "# Stampa i risultati\n",
        "print(valori_unici)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 261
        },
        "id": "CcTDpY1Xj9_J",
        "outputId": "ce40435d-54f6-42f0-d26f-d55c4d92c0e5"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 10646,\n  \"fields\": [\n    {\n      \"column\": \"Rhythm\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 4,\n        \"samples\": [\n          \"SB\",\n          \"GSVT\",\n          \"AFIB\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Beat\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 742,\n        \"samples\": [\n          \"ALS AQW TWC\",\n          \"ARS CCR TWC\",\n          \"APB TWC VPB\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"PatientAge\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 18,\n        \"min\": 4,\n        \"max\": 98,\n        \"num_unique_values\": 95,\n        \"samples\": [\n          7,\n          18,\n          82\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Gender\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"FEMALE\",\n          \"MALE\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"VentricularRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 34,\n        \"min\": 34,\n        \"max\": 263,\n        \"num_unique_values\": 189,\n        \"samples\": [\n          195,\n          211\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"AtrialRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 60,\n        \"min\": 0,\n        \"max\": 535,\n        \"num_unique_values\": 253,\n        \"samples\": [\n          284,\n          86\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSDuration\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 17,\n        \"min\": 18,\n        \"max\": 256,\n        \"num_unique_values\": 80,\n        \"samples\": [\n          122,\n          114\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTInterval\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 59,\n        \"min\": 114,\n        \"max\": 736,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          440,\n          420\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTCorrected\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 39,\n        \"min\": 219,\n        \"max\": 760,\n        \"num_unique_values\": 295,\n        \"samples\": [\n          332,\n          531\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"RAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 41,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 277,\n        \"samples\": [\n          42,\n          -10\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 57,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 356,\n        \"samples\": [\n          101,\n          36\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSCount\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 5,\n        \"min\": 5,\n        \"max\": 40,\n        \"num_unique_values\": 35,\n        \"samples\": [\n          36,\n          25\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOnset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 6,\n        \"min\": 159,\n        \"max\": 240,\n        \"num_unique_values\": 62,\n        \"samples\": [\n          171,\n          170\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 8,\n        \"min\": 249,\n        \"max\": 331,\n        \"num_unique_values\": 68,\n        \"samples\": [\n          283,\n          264\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 29,\n        \"min\": 281,\n        \"max\": 582,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          421,\n          422\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
              "type": "dataframe",
              "variable_name": "df"
            },
            "text/html": [
              "\n",
              "  <div id=\"df-a83f8f77-de5d-472a-b370-ff1097846e0a\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Rhythm</th>\n",
              "      <th>Beat</th>\n",
              "      <th>PatientAge</th>\n",
              "      <th>Gender</th>\n",
              "      <th>VentricularRate</th>\n",
              "      <th>AtrialRate</th>\n",
              "      <th>QRSDuration</th>\n",
              "      <th>QTInterval</th>\n",
              "      <th>QTCorrected</th>\n",
              "      <th>RAxis</th>\n",
              "      <th>TAxis</th>\n",
              "      <th>QRSCount</th>\n",
              "      <th>QOnset</th>\n",
              "      <th>QOffset</th>\n",
              "      <th>TOffset</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>AFIB</td>\n",
              "      <td>RBBB TWC</td>\n",
              "      <td>85</td>\n",
              "      <td>MALE</td>\n",
              "      <td>117</td>\n",
              "      <td>234</td>\n",
              "      <td>114</td>\n",
              "      <td>356</td>\n",
              "      <td>496</td>\n",
              "      <td>81</td>\n",
              "      <td>-27</td>\n",
              "      <td>19</td>\n",
              "      <td>208</td>\n",
              "      <td>265</td>\n",
              "      <td>386</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>SB</td>\n",
              "      <td>TWC</td>\n",
              "      <td>59</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>52</td>\n",
              "      <td>52</td>\n",
              "      <td>92</td>\n",
              "      <td>432</td>\n",
              "      <td>401</td>\n",
              "      <td>76</td>\n",
              "      <td>42</td>\n",
              "      <td>8</td>\n",
              "      <td>215</td>\n",
              "      <td>261</td>\n",
              "      <td>431</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>SR</td>\n",
              "      <td>NONE</td>\n",
              "      <td>20</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>67</td>\n",
              "      <td>67</td>\n",
              "      <td>82</td>\n",
              "      <td>382</td>\n",
              "      <td>403</td>\n",
              "      <td>88</td>\n",
              "      <td>20</td>\n",
              "      <td>11</td>\n",
              "      <td>224</td>\n",
              "      <td>265</td>\n",
              "      <td>415</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>SB</td>\n",
              "      <td>NONE</td>\n",
              "      <td>66</td>\n",
              "      <td>MALE</td>\n",
              "      <td>53</td>\n",
              "      <td>53</td>\n",
              "      <td>96</td>\n",
              "      <td>456</td>\n",
              "      <td>427</td>\n",
              "      <td>34</td>\n",
              "      <td>3</td>\n",
              "      <td>9</td>\n",
              "      <td>219</td>\n",
              "      <td>267</td>\n",
              "      <td>447</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>AFIB</td>\n",
              "      <td>STDD STTC</td>\n",
              "      <td>73</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>162</td>\n",
              "      <td>162</td>\n",
              "      <td>114</td>\n",
              "      <td>252</td>\n",
              "      <td>413</td>\n",
              "      <td>68</td>\n",
              "      <td>-40</td>\n",
              "      <td>26</td>\n",
              "      <td>228</td>\n",
              "      <td>285</td>\n",
              "      <td>354</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-a83f8f77-de5d-472a-b370-ff1097846e0a')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-a83f8f77-de5d-472a-b370-ff1097846e0a button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-a83f8f77-de5d-472a-b370-ff1097846e0a');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-055f8d30-0389-4a2d-90d4-e60e4b93c02d\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-055f8d30-0389-4a2d-90d4-e60e4b93c02d')\"\n",
              "            title=\"Suggest charts\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "      --bg-color: #E8F0FE;\n",
              "      --fill-color: #1967D2;\n",
              "      --hover-bg-color: #E2EBFA;\n",
              "      --hover-fill-color: #174EA6;\n",
              "      --disabled-fill-color: #AAA;\n",
              "      --disabled-bg-color: #DDD;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "      --bg-color: #3B4455;\n",
              "      --fill-color: #D2E3FC;\n",
              "      --hover-bg-color: #434B5C;\n",
              "      --hover-fill-color: #FFFFFF;\n",
              "      --disabled-bg-color: #3B4455;\n",
              "      --disabled-fill-color: #666;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart {\n",
              "    background-color: var(--bg-color);\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: var(--fill-color);\n",
              "    height: 32px;\n",
              "    padding: 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: var(--hover-bg-color);\n",
              "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: var(--button-hover-fill-color);\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart-complete:disabled,\n",
              "  .colab-df-quickchart-complete:disabled:hover {\n",
              "    background-color: var(--disabled-bg-color);\n",
              "    fill: var(--disabled-fill-color);\n",
              "    box-shadow: none;\n",
              "  }\n",
              "\n",
              "  .colab-df-spinner {\n",
              "    border: 2px solid var(--fill-color);\n",
              "    border-color: transparent;\n",
              "    border-bottom-color: var(--fill-color);\n",
              "    animation:\n",
              "      spin 1s steps(1) infinite;\n",
              "  }\n",
              "\n",
              "  @keyframes spin {\n",
              "    0% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "      border-left-color: var(--fill-color);\n",
              "    }\n",
              "    20% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    30% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    40% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    60% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    80% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "    90% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const quickchartButtonEl =\n",
              "        document.querySelector('#' + key + ' button');\n",
              "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
              "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
              "      try {\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      } catch (error) {\n",
              "        console.error('Error during call to suggestCharts:', error);\n",
              "      }\n",
              "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
              "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-055f8d30-0389-4a2d-90d4-e60e4b93c02d button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "  Rhythm       Beat  PatientAge  Gender  VentricularRate  AtrialRate  \\\n",
              "0   AFIB   RBBB TWC          85    MALE              117         234   \n",
              "1     SB        TWC          59  FEMALE               52          52   \n",
              "2     SR       NONE          20  FEMALE               67          67   \n",
              "3     SB       NONE          66    MALE               53          53   \n",
              "4   AFIB  STDD STTC          73  FEMALE              162         162   \n",
              "\n",
              "   QRSDuration  QTInterval  QTCorrected  RAxis  TAxis  QRSCount  QOnset  \\\n",
              "0          114         356          496     81    -27        19     208   \n",
              "1           92         432          401     76     42         8     215   \n",
              "2           82         382          403     88     20        11     224   \n",
              "3           96         456          427     34      3         9     219   \n",
              "4          114         252          413     68    -40        26     228   \n",
              "\n",
              "   QOffset  TOffset  \n",
              "0      265      386  \n",
              "1      261      431  \n",
              "2      265      415  \n",
              "3      267      447  \n",
              "4      285      354  "
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "beat_column = df[\"Beat\"]\n",
        "\n",
        "\n",
        "df = df.drop(columns=[\"FileName\", \"Beat\"])\n",
        "\n",
        "\n",
        "rhythm_mapping = {\n",
        "    \"AFIB\": \"AFIB\",\n",
        "    \"AF\": \"AFIB\",\n",
        "    \"SVT\": \"GSVT\",\n",
        "    \"AT\": \"GSVT\",\n",
        "    \"SAAWR\": \"GSVT\",\n",
        "    \"ST\": \"GSVT\",\n",
        "    \"AVNRT\": \"GSVT\",\n",
        "    \"AVRT\": \"GSVT\",\n",
        "    \"SB\": \"SB\",\n",
        "    \"SR\": \"SR\",\n",
        "    \"SA\": \"SR\"\n",
        "}\n",
        "\n",
        "df[\"Rhythm\"] = df[\"Rhythm\"].replace(rhythm_mapping)\n",
        "\n",
        "df.insert(1, \"Beat\", beat_column)\n",
        "\n",
        "\n",
        "df.head()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fUz0XsWKxBZA",
        "outputId": "fe221ced-4639-47af-b0f5-00d2844a8589"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Rhythm\n",
            "AFIB    2225\n",
            "GSVT    2307\n",
            "SB      3889\n",
            "SR      2225\n",
            "Name: count, dtype: int64\n"
          ]
        }
      ],
      "source": [
        "import pandas as pd\n",
        "\n",
        "\n",
        "\n",
        "# Conta i valori unici nella colonna\n",
        "valori_unici = df[\"Rhythm\"].value_counts().sort_index()\n",
        "\n",
        "# Stampa i risultati\n",
        "print(valori_unici)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "OCoufIk0mvQ6"
      },
      "outputs": [],
      "source": [
        "df = df.drop(columns=[\"Beat\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 226
        },
        "id": "2NQ86M0Ym3GK",
        "outputId": "70dae1b9-e158-43d0-d377-ff0a06beb4a6"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 10646,\n  \"fields\": [\n    {\n      \"column\": \"Rhythm\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 4,\n        \"samples\": [\n          \"SB\",\n          \"GSVT\",\n          \"AFIB\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"PatientAge\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 18,\n        \"min\": 4,\n        \"max\": 98,\n        \"num_unique_values\": 95,\n        \"samples\": [\n          7,\n          18,\n          82\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Gender\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"FEMALE\",\n          \"MALE\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"VentricularRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 34,\n        \"min\": 34,\n        \"max\": 263,\n        \"num_unique_values\": 189,\n        \"samples\": [\n          195,\n          211\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"AtrialRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 60,\n        \"min\": 0,\n        \"max\": 535,\n        \"num_unique_values\": 253,\n        \"samples\": [\n          284,\n          86\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSDuration\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 17,\n        \"min\": 18,\n        \"max\": 256,\n        \"num_unique_values\": 80,\n        \"samples\": [\n          122,\n          114\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTInterval\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 59,\n        \"min\": 114,\n        \"max\": 736,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          440,\n          420\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTCorrected\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 39,\n        \"min\": 219,\n        \"max\": 760,\n        \"num_unique_values\": 295,\n        \"samples\": [\n          332,\n          531\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"RAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 41,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 277,\n        \"samples\": [\n          42,\n          -10\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 57,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 356,\n        \"samples\": [\n          101,\n          36\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSCount\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 5,\n        \"min\": 5,\n        \"max\": 40,\n        \"num_unique_values\": 35,\n        \"samples\": [\n          36,\n          25\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOnset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 6,\n        \"min\": 159,\n        \"max\": 240,\n        \"num_unique_values\": 62,\n        \"samples\": [\n          171,\n          170\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 8,\n        \"min\": 249,\n        \"max\": 331,\n        \"num_unique_values\": 68,\n        \"samples\": [\n          283,\n          264\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 29,\n        \"min\": 281,\n        \"max\": 582,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          421,\n          422\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
              "type": "dataframe",
              "variable_name": "df"
            },
            "text/html": [
              "\n",
              "  <div id=\"df-ca74aa42-fce7-4274-a777-c0c9cbe1ee63\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Rhythm</th>\n",
              "      <th>PatientAge</th>\n",
              "      <th>Gender</th>\n",
              "      <th>VentricularRate</th>\n",
              "      <th>AtrialRate</th>\n",
              "      <th>QRSDuration</th>\n",
              "      <th>QTInterval</th>\n",
              "      <th>QTCorrected</th>\n",
              "      <th>RAxis</th>\n",
              "      <th>TAxis</th>\n",
              "      <th>QRSCount</th>\n",
              "      <th>QOnset</th>\n",
              "      <th>QOffset</th>\n",
              "      <th>TOffset</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>AFIB</td>\n",
              "      <td>85</td>\n",
              "      <td>MALE</td>\n",
              "      <td>117</td>\n",
              "      <td>234</td>\n",
              "      <td>114</td>\n",
              "      <td>356</td>\n",
              "      <td>496</td>\n",
              "      <td>81</td>\n",
              "      <td>-27</td>\n",
              "      <td>19</td>\n",
              "      <td>208</td>\n",
              "      <td>265</td>\n",
              "      <td>386</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>SB</td>\n",
              "      <td>59</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>52</td>\n",
              "      <td>52</td>\n",
              "      <td>92</td>\n",
              "      <td>432</td>\n",
              "      <td>401</td>\n",
              "      <td>76</td>\n",
              "      <td>42</td>\n",
              "      <td>8</td>\n",
              "      <td>215</td>\n",
              "      <td>261</td>\n",
              "      <td>431</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>SR</td>\n",
              "      <td>20</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>67</td>\n",
              "      <td>67</td>\n",
              "      <td>82</td>\n",
              "      <td>382</td>\n",
              "      <td>403</td>\n",
              "      <td>88</td>\n",
              "      <td>20</td>\n",
              "      <td>11</td>\n",
              "      <td>224</td>\n",
              "      <td>265</td>\n",
              "      <td>415</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>SB</td>\n",
              "      <td>66</td>\n",
              "      <td>MALE</td>\n",
              "      <td>53</td>\n",
              "      <td>53</td>\n",
              "      <td>96</td>\n",
              "      <td>456</td>\n",
              "      <td>427</td>\n",
              "      <td>34</td>\n",
              "      <td>3</td>\n",
              "      <td>9</td>\n",
              "      <td>219</td>\n",
              "      <td>267</td>\n",
              "      <td>447</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>AFIB</td>\n",
              "      <td>73</td>\n",
              "      <td>FEMALE</td>\n",
              "      <td>162</td>\n",
              "      <td>162</td>\n",
              "      <td>114</td>\n",
              "      <td>252</td>\n",
              "      <td>413</td>\n",
              "      <td>68</td>\n",
              "      <td>-40</td>\n",
              "      <td>26</td>\n",
              "      <td>228</td>\n",
              "      <td>285</td>\n",
              "      <td>354</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-ca74aa42-fce7-4274-a777-c0c9cbe1ee63')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-ca74aa42-fce7-4274-a777-c0c9cbe1ee63 button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-ca74aa42-fce7-4274-a777-c0c9cbe1ee63');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-d32a4f5c-f644-47df-87fe-a99c17042b8d\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-d32a4f5c-f644-47df-87fe-a99c17042b8d')\"\n",
              "            title=\"Suggest charts\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "      --bg-color: #E8F0FE;\n",
              "      --fill-color: #1967D2;\n",
              "      --hover-bg-color: #E2EBFA;\n",
              "      --hover-fill-color: #174EA6;\n",
              "      --disabled-fill-color: #AAA;\n",
              "      --disabled-bg-color: #DDD;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "      --bg-color: #3B4455;\n",
              "      --fill-color: #D2E3FC;\n",
              "      --hover-bg-color: #434B5C;\n",
              "      --hover-fill-color: #FFFFFF;\n",
              "      --disabled-bg-color: #3B4455;\n",
              "      --disabled-fill-color: #666;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart {\n",
              "    background-color: var(--bg-color);\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: var(--fill-color);\n",
              "    height: 32px;\n",
              "    padding: 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: var(--hover-bg-color);\n",
              "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: var(--button-hover-fill-color);\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart-complete:disabled,\n",
              "  .colab-df-quickchart-complete:disabled:hover {\n",
              "    background-color: var(--disabled-bg-color);\n",
              "    fill: var(--disabled-fill-color);\n",
              "    box-shadow: none;\n",
              "  }\n",
              "\n",
              "  .colab-df-spinner {\n",
              "    border: 2px solid var(--fill-color);\n",
              "    border-color: transparent;\n",
              "    border-bottom-color: var(--fill-color);\n",
              "    animation:\n",
              "      spin 1s steps(1) infinite;\n",
              "  }\n",
              "\n",
              "  @keyframes spin {\n",
              "    0% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "      border-left-color: var(--fill-color);\n",
              "    }\n",
              "    20% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    30% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    40% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    60% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    80% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "    90% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const quickchartButtonEl =\n",
              "        document.querySelector('#' + key + ' button');\n",
              "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
              "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
              "      try {\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      } catch (error) {\n",
              "        console.error('Error during call to suggestCharts:', error);\n",
              "      }\n",
              "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
              "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-d32a4f5c-f644-47df-87fe-a99c17042b8d button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "  Rhythm  PatientAge  Gender  VentricularRate  AtrialRate  QRSDuration  \\\n",
              "0   AFIB          85    MALE              117         234          114   \n",
              "1     SB          59  FEMALE               52          52           92   \n",
              "2     SR          20  FEMALE               67          67           82   \n",
              "3     SB          66    MALE               53          53           96   \n",
              "4   AFIB          73  FEMALE              162         162          114   \n",
              "\n",
              "   QTInterval  QTCorrected  RAxis  TAxis  QRSCount  QOnset  QOffset  TOffset  \n",
              "0         356          496     81    -27        19     208      265      386  \n",
              "1         432          401     76     42         8     215      261      431  \n",
              "2         382          403     88     20        11     224      265      415  \n",
              "3         456          427     34      3         9     219      267      447  \n",
              "4         252          413     68    -40        26     228      285      354  "
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NA4tl8vspG2s",
        "outputId": "1d1dacb3-487d-481f-91ea-41b7c52a8e79"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "      Rhythm  PatientAge  Gender  VentricularRate  AtrialRate  QRSDuration  \\\n",
            "0       AFIB          85    MALE              117         234          114   \n",
            "1         SB          59  FEMALE               52          52           92   \n",
            "2         SR          20  FEMALE               67          67           82   \n",
            "3         SB          66    MALE               53          53           96   \n",
            "4       AFIB          73  FEMALE              162         162          114   \n",
            "...      ...         ...     ...              ...         ...          ...   \n",
            "10641   GSVT          80  FEMALE              196          73          168   \n",
            "10642   GSVT          81  FEMALE              162          81          162   \n",
            "10643   GSVT          39    MALE              152          92          152   \n",
            "10644   GSVT          76    MALE              175         178          128   \n",
            "10645   GSVT          75    MALE              117         104          140   \n",
            "\n",
            "       QTInterval  QTCorrected  RAxis  TAxis  QRSCount  QOnset  QOffset  \\\n",
            "0             356          496     81    -27        19     208      265   \n",
            "1             432          401     76     42         8     215      261   \n",
            "2             382          403     88     20        11     224      265   \n",
            "3             456          427     34      3         9     219      267   \n",
            "4             252          413     68    -40        26     228      285   \n",
            "...           ...          ...    ...    ...       ...     ...      ...   \n",
            "10641         284          513    258    244        32     177      261   \n",
            "10642         294          482    110    -75        27     173      254   \n",
            "10643         340          540    250     38        25     208      284   \n",
            "10644         310          529     98    -83        29     205      269   \n",
            "10645         312          435    263    144        19     208      278   \n",
            "\n",
            "       TOffset  Gender_FEMALE  Gender_MALE  \n",
            "0          386            0.0          1.0  \n",
            "1          431            1.0          0.0  \n",
            "2          415            1.0          0.0  \n",
            "3          447            0.0          1.0  \n",
            "4          354            1.0          0.0  \n",
            "...        ...            ...          ...  \n",
            "10641      319            1.0          0.0  \n",
            "10642      320            1.0          0.0  \n",
            "10643      378            0.0          1.0  \n",
            "10644      360            0.0          1.0  \n",
            "10645      364            0.0          1.0  \n",
            "\n",
            "[10646 rows x 16 columns]\n"
          ]
        }
      ],
      "source": [
        "import pandas as pd\n",
        "from sklearn.preprocessing import OneHotEncoder\n",
        "\n",
        "\n",
        "\n",
        "# Inizializzazione dell'encoder con sparse_output=False\n",
        "encoder = OneHotEncoder(sparse_output=False)\n",
        "\n",
        "# Trasformazione della colonna\n",
        "one_hot = encoder.fit_transform(df[['Gender']])\n",
        "\n",
        "# Creazione del DataFrame con i nomi delle colonne corrette\n",
        "df_encoded = pd.DataFrame(one_hot, columns=encoder.get_feature_names_out(['Gender']))\n",
        "\n",
        "# Concatenazione con il DataFrame originale\n",
        "df = pd.concat([df, df_encoded], axis=1)\n",
        "\n",
        "print(df)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "FE_cdEqEpajE"
      },
      "outputs": [],
      "source": [
        "df = df.drop(columns=[\"Gender\", \"Gender_FEMALE\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 226
        },
        "id": "SlgZIbuLqO0s",
        "outputId": "4ee9001e-1479-4a05-9e7c-e86101c2414e"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "summary": "{\n  \"name\": \"df\",\n  \"rows\": 10646,\n  \"fields\": [\n    {\n      \"column\": \"Rhythm\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 4,\n        \"samples\": [\n          \"SB\",\n          \"GSVT\",\n          \"AFIB\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"PatientAge\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 18,\n        \"min\": 4,\n        \"max\": 98,\n        \"num_unique_values\": 95,\n        \"samples\": [\n          7,\n          18,\n          82\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"VentricularRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 34,\n        \"min\": 34,\n        \"max\": 263,\n        \"num_unique_values\": 189,\n        \"samples\": [\n          195,\n          211,\n          60\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"AtrialRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 60,\n        \"min\": 0,\n        \"max\": 535,\n        \"num_unique_values\": 253,\n        \"samples\": [\n          284,\n          86,\n          48\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSDuration\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 17,\n        \"min\": 18,\n        \"max\": 256,\n        \"num_unique_values\": 80,\n        \"samples\": [\n          122,\n          114,\n          72\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTInterval\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 59,\n        \"min\": 114,\n        \"max\": 736,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          440,\n          420,\n          176\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTCorrected\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 39,\n        \"min\": 219,\n        \"max\": 760,\n        \"num_unique_values\": 295,\n        \"samples\": [\n          332,\n          531,\n          421\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"RAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 41,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 277,\n        \"samples\": [\n          42,\n          -10,\n          -65\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 57,\n        \"min\": -89,\n        \"max\": 270,\n        \"num_unique_values\": 356,\n        \"samples\": [\n          101,\n          36,\n          98\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSCount\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 5,\n        \"min\": 5,\n        \"max\": 40,\n        \"num_unique_values\": 35,\n        \"samples\": [\n          36,\n          25,\n          31\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOnset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 6,\n        \"min\": 159,\n        \"max\": 240,\n        \"num_unique_values\": 62,\n        \"samples\": [\n          171,\n          170,\n          208\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 8,\n        \"min\": 249,\n        \"max\": 331,\n        \"num_unique_values\": 68,\n        \"samples\": [\n          283,\n          264,\n          260\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 29,\n        \"min\": 281,\n        \"max\": 582,\n        \"num_unique_values\": 206,\n        \"samples\": [\n          421,\n          422,\n          318\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Gender_MALE\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.4964753630841413,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          0.0,\n          1.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
              "type": "dataframe",
              "variable_name": "df"
            },
            "text/html": [
              "\n",
              "  <div id=\"df-14df0abf-0634-4414-a85c-7044c745442a\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Rhythm</th>\n",
              "      <th>PatientAge</th>\n",
              "      <th>VentricularRate</th>\n",
              "      <th>AtrialRate</th>\n",
              "      <th>QRSDuration</th>\n",
              "      <th>QTInterval</th>\n",
              "      <th>QTCorrected</th>\n",
              "      <th>RAxis</th>\n",
              "      <th>TAxis</th>\n",
              "      <th>QRSCount</th>\n",
              "      <th>QOnset</th>\n",
              "      <th>QOffset</th>\n",
              "      <th>TOffset</th>\n",
              "      <th>Gender_MALE</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>AFIB</td>\n",
              "      <td>85</td>\n",
              "      <td>117</td>\n",
              "      <td>234</td>\n",
              "      <td>114</td>\n",
              "      <td>356</td>\n",
              "      <td>496</td>\n",
              "      <td>81</td>\n",
              "      <td>-27</td>\n",
              "      <td>19</td>\n",
              "      <td>208</td>\n",
              "      <td>265</td>\n",
              "      <td>386</td>\n",
              "      <td>1.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>SB</td>\n",
              "      <td>59</td>\n",
              "      <td>52</td>\n",
              "      <td>52</td>\n",
              "      <td>92</td>\n",
              "      <td>432</td>\n",
              "      <td>401</td>\n",
              "      <td>76</td>\n",
              "      <td>42</td>\n",
              "      <td>8</td>\n",
              "      <td>215</td>\n",
              "      <td>261</td>\n",
              "      <td>431</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>SR</td>\n",
              "      <td>20</td>\n",
              "      <td>67</td>\n",
              "      <td>67</td>\n",
              "      <td>82</td>\n",
              "      <td>382</td>\n",
              "      <td>403</td>\n",
              "      <td>88</td>\n",
              "      <td>20</td>\n",
              "      <td>11</td>\n",
              "      <td>224</td>\n",
              "      <td>265</td>\n",
              "      <td>415</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>SB</td>\n",
              "      <td>66</td>\n",
              "      <td>53</td>\n",
              "      <td>53</td>\n",
              "      <td>96</td>\n",
              "      <td>456</td>\n",
              "      <td>427</td>\n",
              "      <td>34</td>\n",
              "      <td>3</td>\n",
              "      <td>9</td>\n",
              "      <td>219</td>\n",
              "      <td>267</td>\n",
              "      <td>447</td>\n",
              "      <td>1.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>AFIB</td>\n",
              "      <td>73</td>\n",
              "      <td>162</td>\n",
              "      <td>162</td>\n",
              "      <td>114</td>\n",
              "      <td>252</td>\n",
              "      <td>413</td>\n",
              "      <td>68</td>\n",
              "      <td>-40</td>\n",
              "      <td>26</td>\n",
              "      <td>228</td>\n",
              "      <td>285</td>\n",
              "      <td>354</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-14df0abf-0634-4414-a85c-7044c745442a')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-14df0abf-0634-4414-a85c-7044c745442a button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-14df0abf-0634-4414-a85c-7044c745442a');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-8ee39266-d0f2-4422-b6ed-eca543637cad\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-8ee39266-d0f2-4422-b6ed-eca543637cad')\"\n",
              "            title=\"Suggest charts\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "      --bg-color: #E8F0FE;\n",
              "      --fill-color: #1967D2;\n",
              "      --hover-bg-color: #E2EBFA;\n",
              "      --hover-fill-color: #174EA6;\n",
              "      --disabled-fill-color: #AAA;\n",
              "      --disabled-bg-color: #DDD;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "      --bg-color: #3B4455;\n",
              "      --fill-color: #D2E3FC;\n",
              "      --hover-bg-color: #434B5C;\n",
              "      --hover-fill-color: #FFFFFF;\n",
              "      --disabled-bg-color: #3B4455;\n",
              "      --disabled-fill-color: #666;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart {\n",
              "    background-color: var(--bg-color);\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: var(--fill-color);\n",
              "    height: 32px;\n",
              "    padding: 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: var(--hover-bg-color);\n",
              "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: var(--button-hover-fill-color);\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart-complete:disabled,\n",
              "  .colab-df-quickchart-complete:disabled:hover {\n",
              "    background-color: var(--disabled-bg-color);\n",
              "    fill: var(--disabled-fill-color);\n",
              "    box-shadow: none;\n",
              "  }\n",
              "\n",
              "  .colab-df-spinner {\n",
              "    border: 2px solid var(--fill-color);\n",
              "    border-color: transparent;\n",
              "    border-bottom-color: var(--fill-color);\n",
              "    animation:\n",
              "      spin 1s steps(1) infinite;\n",
              "  }\n",
              "\n",
              "  @keyframes spin {\n",
              "    0% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "      border-left-color: var(--fill-color);\n",
              "    }\n",
              "    20% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    30% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    40% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    60% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    80% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "    90% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const quickchartButtonEl =\n",
              "        document.querySelector('#' + key + ' button');\n",
              "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
              "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
              "      try {\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      } catch (error) {\n",
              "        console.error('Error during call to suggestCharts:', error);\n",
              "      }\n",
              "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
              "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-8ee39266-d0f2-4422-b6ed-eca543637cad button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "  Rhythm  PatientAge  VentricularRate  AtrialRate  QRSDuration  QTInterval  \\\n",
              "0   AFIB          85              117         234          114         356   \n",
              "1     SB          59               52          52           92         432   \n",
              "2     SR          20               67          67           82         382   \n",
              "3     SB          66               53          53           96         456   \n",
              "4   AFIB          73              162         162          114         252   \n",
              "\n",
              "   QTCorrected  RAxis  TAxis  QRSCount  QOnset  QOffset  TOffset  Gender_MALE  \n",
              "0          496     81    -27        19     208      265      386          1.0  \n",
              "1          401     76     42         8     215      261      431          0.0  \n",
              "2          403     88     20        11     224      265      415          0.0  \n",
              "3          427     34      3         9     219      267      447          1.0  \n",
              "4          413     68    -40        26     228      285      354          0.0  "
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "IDAfSTxYrJ4w"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "from sklearn.model_selection import train_test_split\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "c3cFHzO_rOkv",
        "outputId": "092d385c-bea3-4628-c3fe-66150e06cd85"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "(10646, 13) (10646,)\n"
          ]
        }
      ],
      "source": [
        "\n",
        "X = df.drop(columns=['Rhythm'])\n",
        "y = df['Rhythm']\n",
        "\n",
        "print(X.shape, y.shape)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2h0CjBf7qQ9L",
        "outputId": "74e4a052-6698-48f3-8453-280c2461ed25"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "(8516, 13) (2130, 13) (8516,) (2130,)\n"
          ]
        }
      ],
      "source": [
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
        "\n",
        "print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-MYWNan1scF8"
      },
      "outputs": [],
      "source": [
        "from sklearn.preprocessing import MinMaxScaler"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 226
        },
        "id": "8cteJpCqsa1x",
        "outputId": "f121d2bd-53f6-4ad9-c65d-1b93dea3c8cf"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "summary": "{\n  \"name\": \"X_scaled\",\n  \"rows\": 8516,\n  \"fields\": [\n    {\n      \"column\": \"PatientAge\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.19088334675602014,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 95,\n        \"samples\": [\n          0.2978723404255319,\n          0.5319148936170213,\n          0.3404255319148936\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"VentricularRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.15131886961610513,\n        \"min\": 0.0,\n        \"max\": 0.9999999999999999,\n        \"num_unique_values\": 189,\n        \"samples\": [\n          0.7816593886462881,\n          0.5152838427947598,\n          0.2445414847161572\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"AtrialRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.12036299099081324,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 248,\n        \"samples\": [\n          0.17400000000000002,\n          0.10400000000000001,\n          0.28\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSDuration\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.08570027372205993,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 75,\n        \"samples\": [\n          0.21568627450980393,\n          0.5686274509803921,\n          0.2254901960784314\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTInterval\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.10270224319439204,\n        \"min\": 0.0,\n        \"max\": 0.9999999999999999,\n        \"num_unique_values\": 194,\n        \"samples\": [\n          0.31010452961672474,\n          0.5226480836236933,\n          0.15679442508710803\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTCorrected\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.07229748830376431,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 275,\n        \"samples\": [\n          0.2735674676524954,\n          0.36598890942698703,\n          0.23844731977818856\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"RAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.11495989754583597,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 267,\n        \"samples\": [\n          0.22625698324022347,\n          0.11173184357541902,\n          0.1229050279329609\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.1598158000198737,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 350,\n        \"samples\": [\n          0.21448467966573817,\n          0.7242339832869081,\n          0.07799442896935935\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSCount\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.16308008949506902,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 35,\n        \"samples\": [\n          0.6285714285714286,\n          0.7428571428571429,\n          0.7142857142857142\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOnset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.08320750697325857,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 57,\n        \"samples\": [\n          0.8552631578947367,\n          0.6184210526315788,\n          0.6052631578947367\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.10295192890605691,\n        \"min\": 0.0,\n        \"max\": 1.0000000000000004,\n        \"num_unique_values\": 67,\n        \"samples\": [\n          0.4878048780487805,\n          0.17073170731707332,\n          0.1585365853658538\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.10452709517418823,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 197,\n        \"samples\": [\n          0.7526501766784452,\n          0.5759717314487633,\n          0.46289752650176674\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Gender_MALE\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.4966390491716783,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          0.0,\n          1.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
              "type": "dataframe",
              "variable_name": "X_scaled"
            },
            "text/html": [
              "\n",
              "  <div id=\"df-ea99c96e-3b99-48fb-9005-2ee03e11162c\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PatientAge</th>\n",
              "      <th>VentricularRate</th>\n",
              "      <th>AtrialRate</th>\n",
              "      <th>QRSDuration</th>\n",
              "      <th>QTInterval</th>\n",
              "      <th>QTCorrected</th>\n",
              "      <th>RAxis</th>\n",
              "      <th>TAxis</th>\n",
              "      <th>QRSCount</th>\n",
              "      <th>QOnset</th>\n",
              "      <th>QOffset</th>\n",
              "      <th>TOffset</th>\n",
              "      <th>Gender_MALE</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>4842</th>\n",
              "      <td>0.861702</td>\n",
              "      <td>0.344978</td>\n",
              "      <td>0.468</td>\n",
              "      <td>0.274510</td>\n",
              "      <td>0.376307</td>\n",
              "      <td>0.430684</td>\n",
              "      <td>0.195531</td>\n",
              "      <td>0.339833</td>\n",
              "      <td>0.400000</td>\n",
              "      <td>0.855263</td>\n",
              "      <td>0.353659</td>\n",
              "      <td>0.381625</td>\n",
              "      <td>1.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7234</th>\n",
              "      <td>0.840426</td>\n",
              "      <td>0.580786</td>\n",
              "      <td>0.348</td>\n",
              "      <td>0.068627</td>\n",
              "      <td>0.261324</td>\n",
              "      <td>0.408503</td>\n",
              "      <td>0.371508</td>\n",
              "      <td>0.559889</td>\n",
              "      <td>0.657143</td>\n",
              "      <td>0.894737</td>\n",
              "      <td>0.134146</td>\n",
              "      <td>0.275618</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9283</th>\n",
              "      <td>0.574468</td>\n",
              "      <td>0.109170</td>\n",
              "      <td>0.118</td>\n",
              "      <td>0.186275</td>\n",
              "      <td>0.567944</td>\n",
              "      <td>0.399261</td>\n",
              "      <td>0.399441</td>\n",
              "      <td>0.373259</td>\n",
              "      <td>0.142857</td>\n",
              "      <td>0.815789</td>\n",
              "      <td>0.207317</td>\n",
              "      <td>0.565371</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7273</th>\n",
              "      <td>0.468085</td>\n",
              "      <td>0.109170</td>\n",
              "      <td>0.118</td>\n",
              "      <td>0.166667</td>\n",
              "      <td>0.672474</td>\n",
              "      <td>0.510166</td>\n",
              "      <td>0.405028</td>\n",
              "      <td>0.367688</td>\n",
              "      <td>0.142857</td>\n",
              "      <td>0.855263</td>\n",
              "      <td>0.219512</td>\n",
              "      <td>0.681979</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2629</th>\n",
              "      <td>0.617021</td>\n",
              "      <td>0.174672</td>\n",
              "      <td>0.148</td>\n",
              "      <td>0.215686</td>\n",
              "      <td>0.543554</td>\n",
              "      <td>0.467652</td>\n",
              "      <td>0.262570</td>\n",
              "      <td>0.309192</td>\n",
              "      <td>0.200000</td>\n",
              "      <td>0.723684</td>\n",
              "      <td>0.158537</td>\n",
              "      <td>0.515901</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-ea99c96e-3b99-48fb-9005-2ee03e11162c')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-ea99c96e-3b99-48fb-9005-2ee03e11162c button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-ea99c96e-3b99-48fb-9005-2ee03e11162c');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-38904078-f104-44b4-8160-0c9d2fc28499\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-38904078-f104-44b4-8160-0c9d2fc28499')\"\n",
              "            title=\"Suggest charts\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "      --bg-color: #E8F0FE;\n",
              "      --fill-color: #1967D2;\n",
              "      --hover-bg-color: #E2EBFA;\n",
              "      --hover-fill-color: #174EA6;\n",
              "      --disabled-fill-color: #AAA;\n",
              "      --disabled-bg-color: #DDD;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "      --bg-color: #3B4455;\n",
              "      --fill-color: #D2E3FC;\n",
              "      --hover-bg-color: #434B5C;\n",
              "      --hover-fill-color: #FFFFFF;\n",
              "      --disabled-bg-color: #3B4455;\n",
              "      --disabled-fill-color: #666;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart {\n",
              "    background-color: var(--bg-color);\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: var(--fill-color);\n",
              "    height: 32px;\n",
              "    padding: 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: var(--hover-bg-color);\n",
              "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: var(--button-hover-fill-color);\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart-complete:disabled,\n",
              "  .colab-df-quickchart-complete:disabled:hover {\n",
              "    background-color: var(--disabled-bg-color);\n",
              "    fill: var(--disabled-fill-color);\n",
              "    box-shadow: none;\n",
              "  }\n",
              "\n",
              "  .colab-df-spinner {\n",
              "    border: 2px solid var(--fill-color);\n",
              "    border-color: transparent;\n",
              "    border-bottom-color: var(--fill-color);\n",
              "    animation:\n",
              "      spin 1s steps(1) infinite;\n",
              "  }\n",
              "\n",
              "  @keyframes spin {\n",
              "    0% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "      border-left-color: var(--fill-color);\n",
              "    }\n",
              "    20% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    30% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    40% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    60% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    80% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "    90% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const quickchartButtonEl =\n",
              "        document.querySelector('#' + key + ' button');\n",
              "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
              "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
              "      try {\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      } catch (error) {\n",
              "        console.error('Error during call to suggestCharts:', error);\n",
              "      }\n",
              "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
              "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-38904078-f104-44b4-8160-0c9d2fc28499 button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "      PatientAge  VentricularRate  AtrialRate  QRSDuration  QTInterval  \\\n",
              "4842    0.861702         0.344978       0.468     0.274510    0.376307   \n",
              "7234    0.840426         0.580786       0.348     0.068627    0.261324   \n",
              "9283    0.574468         0.109170       0.118     0.186275    0.567944   \n",
              "7273    0.468085         0.109170       0.118     0.166667    0.672474   \n",
              "2629    0.617021         0.174672       0.148     0.215686    0.543554   \n",
              "\n",
              "      QTCorrected     RAxis     TAxis  QRSCount    QOnset   QOffset   TOffset  \\\n",
              "4842     0.430684  0.195531  0.339833  0.400000  0.855263  0.353659  0.381625   \n",
              "7234     0.408503  0.371508  0.559889  0.657143  0.894737  0.134146  0.275618   \n",
              "9283     0.399261  0.399441  0.373259  0.142857  0.815789  0.207317  0.565371   \n",
              "7273     0.510166  0.405028  0.367688  0.142857  0.855263  0.219512  0.681979   \n",
              "2629     0.467652  0.262570  0.309192  0.200000  0.723684  0.158537  0.515901   \n",
              "\n",
              "      Gender_MALE  \n",
              "4842          1.0  \n",
              "7234          0.0  \n",
              "9283          0.0  \n",
              "7273          0.0  \n",
              "2629          0.0  "
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Applica la scalatura tra -1 e 1\n",
        "scaler = MinMaxScaler(feature_range=(0, 1))\n",
        "X_scaled = scaler.fit_transform(X_train)\n",
        "# Convert the scaled NumPy array back to a pandas DataFrame\n",
        "X_scaled = pd.DataFrame(X_scaled, columns=X_train.columns, index=X_train.index)  # Keep original columns and index\n",
        "\n",
        "# Now you can use head()\n",
        "X_scaled.head()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "8vwIwAn9s-Ke"
      },
      "outputs": [],
      "source": [
        "X_scaled_test = scaler.transform(X_test)\n",
        "# Convert the scaled NumPy array back to a pandas DataFrame\n",
        "X_scaled_test = pd.DataFrame(X_scaled_test, columns=X_test.columns, index=X_test.index)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 226
        },
        "id": "gSAI8OY4s_0B",
        "outputId": "bf89d776-c4cd-402d-9777-0aa48581e44f"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "summary": "{\n  \"name\": \"X_scaled_test\",\n  \"rows\": 2130,\n  \"fields\": [\n    {\n      \"column\": \"PatientAge\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.1954911378496646,\n        \"min\": 0.0,\n        \"max\": 0.9893617021276596,\n        \"num_unique_values\": 93,\n        \"samples\": [\n          0.07446808510638298,\n          0.7872340425531915,\n          0.3936170212765957\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"VentricularRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.14699018634306557,\n        \"min\": 0.02620087336244542,\n        \"max\": 0.8209606986899562,\n        \"num_unique_values\": 158,\n        \"samples\": [\n          0.6768558951965065,\n          0.3406113537117904,\n          0.6462882096069869\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"AtrialRate\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.11951333128238915,\n        \"min\": 0.0,\n        \"max\": 1.07,\n        \"num_unique_values\": 177,\n        \"samples\": [\n          0.158,\n          0.258,\n          0.41400000000000003\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSDuration\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.08563850697390331,\n        \"min\": -0.16666666666666666,\n        \"max\": 0.8823529411764706,\n        \"num_unique_values\": 66,\n        \"samples\": [\n          0.48039215686274506,\n          0.4901960784313726,\n          0.0980392156862745\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTInterval\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.10510012649446764,\n        \"min\": 0.0592334494773519,\n        \"max\": 1.083623693379791,\n        \"num_unique_values\": 173,\n        \"samples\": [\n          0.10801393728222994,\n          0.313588850174216,\n          0.35540069686411146\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QTCorrected\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.07450552827793877,\n        \"min\": 0.009242144177449174,\n        \"max\": 0.8336414048059149,\n        \"num_unique_values\": 219,\n        \"samples\": [\n          0.6968576709796672,\n          0.32532347504621073,\n          0.5970425138632163\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"RAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.11577537811634964,\n        \"min\": -0.002793296089385472,\n        \"max\": 0.9804469273743017,\n        \"num_unique_values\": 207,\n        \"samples\": [\n          0.05865921787709499,\n          0.12569832402234637,\n          0.5083798882681565\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TAxis\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.16121718694979764,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 292,\n        \"samples\": [\n          0.5236768802228412,\n          0.9526462395543176,\n          0.21448467966573817\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QRSCount\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.15854797473874666,\n        \"min\": 0.02857142857142858,\n        \"max\": 0.8857142857142857,\n        \"num_unique_values\": 31,\n        \"samples\": [\n          0.02857142857142858,\n          0.4857142857142857,\n          0.8\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOnset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.08873558040792456,\n        \"min\": 0.0,\n        \"max\": 1.0657894736842106,\n        \"num_unique_values\": 48,\n        \"samples\": [\n          0.9473684210526314,\n          0.3421052631578947,\n          0.48684210526315796\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"QOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.09796033332853174,\n        \"min\": 0.0,\n        \"max\": 0.7195121951219514,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          0.07317073170731714,\n          0.2682926829268295,\n          0.6097560975609757\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"TOffset\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.10797303527159131,\n        \"min\": 0.06713780918727918,\n        \"max\": 1.0636042402826855,\n        \"num_unique_values\": 174,\n        \"samples\": [\n          0.20494699646643122,\n          0.6183745583038869,\n          0.29681978798586584\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Gender_MALE\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.49590107335630734,\n        \"min\": 0.0,\n        \"max\": 1.0,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          1.0,\n          0.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
              "type": "dataframe",
              "variable_name": "X_scaled_test"
            },
            "text/html": [
              "\n",
              "  <div id=\"df-f4fb5d02-45dd-42e4-8616-3e923d8062e6\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>PatientAge</th>\n",
              "      <th>VentricularRate</th>\n",
              "      <th>AtrialRate</th>\n",
              "      <th>QRSDuration</th>\n",
              "      <th>QTInterval</th>\n",
              "      <th>QTCorrected</th>\n",
              "      <th>RAxis</th>\n",
              "      <th>TAxis</th>\n",
              "      <th>QRSCount</th>\n",
              "      <th>QOnset</th>\n",
              "      <th>QOffset</th>\n",
              "      <th>TOffset</th>\n",
              "      <th>Gender_MALE</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>107</th>\n",
              "      <td>0.595745</td>\n",
              "      <td>0.104803</td>\n",
              "      <td>0.116</td>\n",
              "      <td>0.098039</td>\n",
              "      <td>0.592334</td>\n",
              "      <td>0.417745</td>\n",
              "      <td>0.310056</td>\n",
              "      <td>0.331476</td>\n",
              "      <td>0.142857</td>\n",
              "      <td>0.789474</td>\n",
              "      <td>0.073171</td>\n",
              "      <td>0.583039</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3573</th>\n",
              "      <td>0.680851</td>\n",
              "      <td>0.144105</td>\n",
              "      <td>0.500</td>\n",
              "      <td>0.117647</td>\n",
              "      <td>0.641115</td>\n",
              "      <td>0.536044</td>\n",
              "      <td>0.455307</td>\n",
              "      <td>0.476323</td>\n",
              "      <td>0.171429</td>\n",
              "      <td>0.789474</td>\n",
              "      <td>0.097561</td>\n",
              "      <td>0.632509</td>\n",
              "      <td>1.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>10121</th>\n",
              "      <td>0.414894</td>\n",
              "      <td>0.615721</td>\n",
              "      <td>0.356</td>\n",
              "      <td>0.039216</td>\n",
              "      <td>0.254355</td>\n",
              "      <td>0.414048</td>\n",
              "      <td>0.441341</td>\n",
              "      <td>0.278552</td>\n",
              "      <td>0.657143</td>\n",
              "      <td>0.934211</td>\n",
              "      <td>0.134146</td>\n",
              "      <td>0.279152</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3139</th>\n",
              "      <td>0.840426</td>\n",
              "      <td>0.344978</td>\n",
              "      <td>0.226</td>\n",
              "      <td>0.117647</td>\n",
              "      <td>0.386760</td>\n",
              "      <td>0.445471</td>\n",
              "      <td>0.310056</td>\n",
              "      <td>0.378830</td>\n",
              "      <td>0.371429</td>\n",
              "      <td>0.828947</td>\n",
              "      <td>0.134146</td>\n",
              "      <td>0.385159</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9031</th>\n",
              "      <td>0.617021</td>\n",
              "      <td>0.427948</td>\n",
              "      <td>0.264</td>\n",
              "      <td>0.215686</td>\n",
              "      <td>0.369338</td>\n",
              "      <td>0.487985</td>\n",
              "      <td>0.231844</td>\n",
              "      <td>0.398329</td>\n",
              "      <td>0.457143</td>\n",
              "      <td>0.789474</td>\n",
              "      <td>0.219512</td>\n",
              "      <td>0.356890</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-f4fb5d02-45dd-42e4-8616-3e923d8062e6')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-f4fb5d02-45dd-42e4-8616-3e923d8062e6 button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-f4fb5d02-45dd-42e4-8616-3e923d8062e6');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-32945893-3189-4f31-8b2e-32159f239d15\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-32945893-3189-4f31-8b2e-32159f239d15')\"\n",
              "            title=\"Suggest charts\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "      --bg-color: #E8F0FE;\n",
              "      --fill-color: #1967D2;\n",
              "      --hover-bg-color: #E2EBFA;\n",
              "      --hover-fill-color: #174EA6;\n",
              "      --disabled-fill-color: #AAA;\n",
              "      --disabled-bg-color: #DDD;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "      --bg-color: #3B4455;\n",
              "      --fill-color: #D2E3FC;\n",
              "      --hover-bg-color: #434B5C;\n",
              "      --hover-fill-color: #FFFFFF;\n",
              "      --disabled-bg-color: #3B4455;\n",
              "      --disabled-fill-color: #666;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart {\n",
              "    background-color: var(--bg-color);\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: var(--fill-color);\n",
              "    height: 32px;\n",
              "    padding: 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: var(--hover-bg-color);\n",
              "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: var(--button-hover-fill-color);\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart-complete:disabled,\n",
              "  .colab-df-quickchart-complete:disabled:hover {\n",
              "    background-color: var(--disabled-bg-color);\n",
              "    fill: var(--disabled-fill-color);\n",
              "    box-shadow: none;\n",
              "  }\n",
              "\n",
              "  .colab-df-spinner {\n",
              "    border: 2px solid var(--fill-color);\n",
              "    border-color: transparent;\n",
              "    border-bottom-color: var(--fill-color);\n",
              "    animation:\n",
              "      spin 1s steps(1) infinite;\n",
              "  }\n",
              "\n",
              "  @keyframes spin {\n",
              "    0% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "      border-left-color: var(--fill-color);\n",
              "    }\n",
              "    20% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    30% {\n",
              "      border-color: transparent;\n",
              "      border-left-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    40% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-top-color: var(--fill-color);\n",
              "    }\n",
              "    60% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "    }\n",
              "    80% {\n",
              "      border-color: transparent;\n",
              "      border-right-color: var(--fill-color);\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "    90% {\n",
              "      border-color: transparent;\n",
              "      border-bottom-color: var(--fill-color);\n",
              "    }\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const quickchartButtonEl =\n",
              "        document.querySelector('#' + key + ' button');\n",
              "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
              "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
              "      try {\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      } catch (error) {\n",
              "        console.error('Error during call to suggestCharts:', error);\n",
              "      }\n",
              "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
              "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-32945893-3189-4f31-8b2e-32159f239d15 button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "       PatientAge  VentricularRate  AtrialRate  QRSDuration  QTInterval  \\\n",
              "107      0.595745         0.104803       0.116     0.098039    0.592334   \n",
              "3573     0.680851         0.144105       0.500     0.117647    0.641115   \n",
              "10121    0.414894         0.615721       0.356     0.039216    0.254355   \n",
              "3139     0.840426         0.344978       0.226     0.117647    0.386760   \n",
              "9031     0.617021         0.427948       0.264     0.215686    0.369338   \n",
              "\n",
              "       QTCorrected     RAxis     TAxis  QRSCount    QOnset   QOffset  \\\n",
              "107       0.417745  0.310056  0.331476  0.142857  0.789474  0.073171   \n",
              "3573      0.536044  0.455307  0.476323  0.171429  0.789474  0.097561   \n",
              "10121     0.414048  0.441341  0.278552  0.657143  0.934211  0.134146   \n",
              "3139      0.445471  0.310056  0.378830  0.371429  0.828947  0.134146   \n",
              "9031      0.487985  0.231844  0.398329  0.457143  0.789474  0.219512   \n",
              "\n",
              "        TOffset  Gender_MALE  \n",
              "107    0.583039          0.0  \n",
              "3573   0.632509          1.0  \n",
              "10121  0.279152          0.0  \n",
              "3139   0.385159          0.0  \n",
              "9031   0.356890          0.0  "
            ]
          },
          "execution_count": 19,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "X_scaled_test.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "S1m0QOLitYcD"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1kjK3T0zvJl-"
      },
      "source": [
        "SENZA SMOTE SENZA POSITIONAL SCALE"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "KWa9TxfO1pIo",
        "outputId": "0e2da35f-6344-4e7d-b2fb-f0e0be7aabdb"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Class Counts:\n",
            "0    3112\n",
            "2    3112\n",
            "3    3112\n",
            "1    3112\n",
            "Name: count, dtype: int64\n",
            "Scale Pos Weight: 3.0\n",
            "Fitting 3 folds for each of 50 candidates, totalling 150 fits\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.11/dist-packages/xgboost/core.py:158: UserWarning: [10:30:02] WARNING: /workspace/src/learner.cc:740: \n",
            "Parameters: { \"scale_pos_weight\", \"use_label_encoder\" } are not used.\n",
            "\n",
            "  warnings.warn(smsg, UserWarning)\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "F1-Score (weighted) XGBoost: 0.9168\n",
            "\n",
            "Classification Report (XGBoost):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "        AFIB       0.83      0.79      0.81       424\n",
            "        GSVT       0.87      0.88      0.87       482\n",
            "          SB       0.98      0.99      0.98       777\n",
            "          SR       0.94      0.96      0.95       447\n",
            "\n",
            "    accuracy                           0.92      2130\n",
            "   macro avg       0.90      0.90      0.90      2130\n",
            "weighted avg       0.92      0.92      0.92      2130\n",
            "\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from imblearn.over_sampling import SMOTE\n",
        "from sklearn.preprocessing import LabelEncoder\n",
        "from xgboost import XGBClassifier\n",
        "from sklearn.model_selection import RandomizedSearchCV\n",
        "from sklearn.metrics import f1_score, classification_report\n",
        "\n",
        "# 1️⃣ **Bilanciamento con SMOTE**\n",
        "smote = SMOTE(random_state=42)\n",
        "X_train_balanced, y_train_balanced = smote.fit_resample(X_train, y_train)\n",
        "\n",
        "# 2️⃣ **Codifica delle etichette**\n",
        "label_encoder = LabelEncoder()\n",
        "y_train_encoded = label_encoder.fit_transform(y_train_balanced)\n",
        "y_test_encoded = label_encoder.transform(y_test)\n",
        "\n",
        "# 3️⃣ **Correzione dell'errore numpy → Pandas**\n",
        "class_counts = pd.Series(y_train_encoded).value_counts()\n",
        "\n",
        "# 4️⃣ **Calcolo automatico di scale_pos_weight** (singolo valore per XGBoost)\n",
        "# Calcola il \"scale_pos_weight\" come il rapporto tra il numero di campioni negativi e positivi\n",
        "scale_pos_weight = (class_counts.sum() - class_counts[1]) / class_counts[1]  # Solo per la classe positiva\n",
        "\n",
        "print(f\"Class Counts:\\n{class_counts}\")\n",
        "print(f\"Scale Pos Weight: {scale_pos_weight}\")\n",
        "\n",
        "# 5️⃣ **Definizione del modello XGBoost**\n",
        "xgb_model = XGBClassifier(\n",
        "    random_state=42,\n",
        "    use_label_encoder=False,\n",
        "    eval_metric='mlogloss',\n",
        "    scale_pos_weight=scale_pos_weight  # Passiamo il singolo valore calcolato\n",
        ")\n",
        "\n",
        "# 6️⃣ **Ricerca degli iperparametri con RandomizedSearchCV**\n",
        "param_dist_xgb = {\n",
        "    'n_estimators': [100, 200, 300, 400, 500],\n",
        "    'learning_rate': [0.01, 0.05, 0.1, 0.15],\n",
        "    'max_depth': [3, 4, 5, 6, 7],\n",
        "    'min_child_weight': [1, 3, 5, 7],\n",
        "    'subsample': [0.7, 0.8, 0.9, 1.0],\n",
        "    'colsample_bytree': [0.7, 0.8, 0.9, 1.0],\n",
        "    'gamma': [0, 0.1, 0.2, 0.3]\n",
        "}\n",
        "\n",
        "random_search_xgb = RandomizedSearchCV(\n",
        "    xgb_model,\n",
        "    param_distributions=param_dist_xgb,\n",
        "    n_iter=50,\n",
        "    scoring='f1_macro',\n",
        "    n_jobs=-1,\n",
        "    cv=3,\n",
        "    verbose=2,\n",
        "    random_state=42\n",
        ")\n",
        "\n",
        "# 7️⃣ **Addestramento del modello**\n",
        "random_search_xgb.fit(X_train_balanced, y_train_encoded)  # Usa i dati bilanciati\n",
        "\n",
        "# 8️⃣ **Miglior modello trovato**\n",
        "best_xgb_model = random_search_xgb.best_estimator_\n",
        "\n",
        "# 9️⃣ **Predizioni sul test set**\n",
        "y_pred_xgb = best_xgb_model.predict(X_test)\n",
        "\n",
        "# 🔟 **Calcolo delle metriche di valutazione**\n",
        "f1_xgb = f1_score(y_test_encoded, y_pred_xgb, average='weighted')\n",
        "print(f\"\\nF1-Score (weighted) XGBoost: {f1_xgb:.4f}\")\n",
        "\n",
        "print(\"\\nClassification Report (XGBoost):\")\n",
        "print(classification_report(y_test_encoded, y_pred_xgb, target_names=label_encoder.classes_))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SirMLUXX2yhQ",
        "outputId": "0c72ab5e-cfe8-44a0-a122-44579da7b36d"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Fitting 3 folds for each of 50 candidates, totalling 150 fits\n",
            "F1-Score (weighted) Random Forest: 0.9011\n",
            "Classification Report (Random Forest):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "        AFIB       0.79      0.75      0.77       424\n",
            "        GSVT       0.85      0.87      0.86       482\n",
            "          SB       0.98      0.99      0.99       777\n",
            "          SR       0.92      0.93      0.93       447\n",
            "\n",
            "    accuracy                           0.90      2130\n",
            "   macro avg       0.89      0.88      0.88      2130\n",
            "weighted avg       0.90      0.90      0.90      2130\n",
            "\n"
          ]
        }
      ],
      "source": [
        "from imblearn.over_sampling import SMOTE\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "from sklearn.model_selection import RandomizedSearchCV\n",
        "from sklearn.metrics import f1_score, classification_report\n",
        "\n",
        "# 1. Applica SMOTE per bilanciare il dataset di addestramento\n",
        "#smote = SMOTE(random_state=42)\n",
        "#X_train_balanced, y_train_balanced = smote.fit_resample(X_train, y_train)\n",
        "\n",
        "# 2. Definiamo il modello Random Forest\n",
        "rf = RandomForestClassifier(random_state=42)\n",
        "\n",
        "# Parametri da esplorare per RandomizedSearchCV\n",
        "param_dist_rf = {\n",
        "    'n_estimators': [100, 200, 300, 400, 500],\n",
        "    'max_depth': [None, 10, 20, 30, 40, 50],\n",
        "    'min_samples_split': [2, 5, 10, 20],\n",
        "    'min_samples_leaf': [1, 2, 4, 6],\n",
        "    'max_features': ['sqrt', 'log2'],\n",
        "    'bootstrap': [True, False],\n",
        "}\n",
        "\n",
        "# 3. RandomizedSearchCV per trovare la combinazione migliore di parametri\n",
        "random_search_rf = RandomizedSearchCV(\n",
        "    rf,\n",
        "    param_distributions=param_dist_rf,\n",
        "    n_iter=50,  # Numero di combinazioni da provare\n",
        "    scoring='accuracy',\n",
        "    n_jobs=-1,  # Usa tutte le risorse della CPU\n",
        "    cv=3,  # 3-fold cross-validation\n",
        "    verbose=2,  # Mostra il progresso della ricerca\n",
        "    random_state=42\n",
        ")\n",
        "\n",
        "# 4. Eseguiamo la ricerca per trovare la miglior combinazione di parametri\n",
        "random_search_rf.fit(X_train, y_train)\n",
        "\n",
        "# 5. Otteniamo il miglior modello da RandomizedSearchCV\n",
        "best_rf_model = random_search_rf.best_estimator_\n",
        "\n",
        "# 6. Predizioni sul test set\n",
        "y_pred_rf = best_rf_model.predict(X_test)\n",
        "\n",
        "# 7. Calcolare l'F1-Score\n",
        "f1_rf = f1_score(y_test, y_pred_rf, average='weighted')  # F1-score ponderato\n",
        "print(f\"F1-Score (weighted) Random Forest: {f1_rf:.4f}\")\n",
        "\n",
        "# 8. Stampa il classification report\n",
        "print(\"Classification Report (Random Forest):\")\n",
        "print(classification_report(y_test, y_pred_rf))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZEhFEF4h2_nF",
        "outputId": "92b9db90-3b43-47b5-c435-5fbcc438695a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Collecting pytorch-tabnet\n",
            "  Downloading pytorch_tabnet-4.1.0-py3-none-any.whl.metadata (15 kB)\n",
            "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.11/dist-packages (from pytorch-tabnet) (2.0.2)\n",
            "Requirement already satisfied: scikit_learn>0.21 in /usr/local/lib/python3.11/dist-packages (from pytorch-tabnet) (1.6.1)\n",
            "Requirement already satisfied: scipy>1.4 in /usr/local/lib/python3.11/dist-packages (from pytorch-tabnet) (1.14.1)\n",
            "Requirement already satisfied: torch>=1.3 in /usr/local/lib/python3.11/dist-packages (from pytorch-tabnet) (2.6.0+cu124)\n",
            "Requirement already satisfied: tqdm>=4.36 in /usr/local/lib/python3.11/dist-packages (from pytorch-tabnet) (4.67.1)\n",
            "Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.11/dist-packages (from scikit_learn>0.21->pytorch-tabnet) (1.4.2)\n",
            "Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from scikit_learn>0.21->pytorch-tabnet) (3.6.0)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (3.18.0)\n",
            "Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (4.12.2)\n",
            "Requirement already satisfied: networkx in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (3.4.2)\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (3.1.6)\n",
            "Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (2025.3.0)\n",
            "Collecting nvidia-cuda-nvrtc-cu12==12.4.127 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
            "Collecting nvidia-cuda-runtime-cu12==12.4.127 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
            "Collecting nvidia-cuda-cupti-cu12==12.4.127 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
            "Collecting nvidia-cudnn-cu12==9.1.0.70 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
            "Collecting nvidia-cublas-cu12==12.4.5.8 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
            "Collecting nvidia-cufft-cu12==11.2.1.3 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
            "Collecting nvidia-curand-cu12==10.3.5.147 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
            "Collecting nvidia-cusolver-cu12==11.6.1.9 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
            "Collecting nvidia-cusparse-cu12==12.3.1.170 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
            "Requirement already satisfied: nvidia-cusparselt-cu12==0.6.2 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (0.6.2)\n",
            "Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (2.21.5)\n",
            "Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (12.4.127)\n",
            "Collecting nvidia-nvjitlink-cu12==12.4.127 (from torch>=1.3->pytorch-tabnet)\n",
            "  Downloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
            "Requirement already satisfied: triton==3.2.0 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (3.2.0)\n",
            "Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.11/dist-packages (from torch>=1.3->pytorch-tabnet) (1.13.1)\n",
            "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy==1.13.1->torch>=1.3->pytorch-tabnet) (1.3.0)\n",
            "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->torch>=1.3->pytorch-tabnet) (3.0.2)\n",
            "Downloading pytorch_tabnet-4.1.0-py3-none-any.whl (44 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.5/44.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl (363.4 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (13.8 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m58.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (24.6 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m49.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (883 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m37.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m1.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl (211.5 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl (56.3 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m19.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl (127.9 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl (207.5 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m59.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hInstalling collected packages: nvidia-nvjitlink-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, nvidia-cusparse-cu12, nvidia-cudnn-cu12, nvidia-cusolver-cu12, pytorch-tabnet\n",
            "  Attempting uninstall: nvidia-nvjitlink-cu12\n",
            "    Found existing installation: nvidia-nvjitlink-cu12 12.5.82\n",
            "    Uninstalling nvidia-nvjitlink-cu12-12.5.82:\n",
            "      Successfully uninstalled nvidia-nvjitlink-cu12-12.5.82\n",
            "  Attempting uninstall: nvidia-curand-cu12\n",
            "    Found existing installation: nvidia-curand-cu12 10.3.6.82\n",
            "    Uninstalling nvidia-curand-cu12-10.3.6.82:\n",
            "      Successfully uninstalled nvidia-curand-cu12-10.3.6.82\n",
            "  Attempting uninstall: nvidia-cufft-cu12\n",
            "    Found existing installation: nvidia-cufft-cu12 11.2.3.61\n",
            "    Uninstalling nvidia-cufft-cu12-11.2.3.61:\n",
            "      Successfully uninstalled nvidia-cufft-cu12-11.2.3.61\n",
            "  Attempting uninstall: nvidia-cuda-runtime-cu12\n",
            "    Found existing installation: nvidia-cuda-runtime-cu12 12.5.82\n",
            "    Uninstalling nvidia-cuda-runtime-cu12-12.5.82:\n",
            "      Successfully uninstalled nvidia-cuda-runtime-cu12-12.5.82\n",
            "  Attempting uninstall: nvidia-cuda-nvrtc-cu12\n",
            "    Found existing installation: nvidia-cuda-nvrtc-cu12 12.5.82\n",
            "    Uninstalling nvidia-cuda-nvrtc-cu12-12.5.82:\n",
            "      Successfully uninstalled nvidia-cuda-nvrtc-cu12-12.5.82\n",
            "  Attempting uninstall: nvidia-cuda-cupti-cu12\n",
            "    Found existing installation: nvidia-cuda-cupti-cu12 12.5.82\n",
            "    Uninstalling nvidia-cuda-cupti-cu12-12.5.82:\n",
            "      Successfully uninstalled nvidia-cuda-cupti-cu12-12.5.82\n",
            "  Attempting uninstall: nvidia-cublas-cu12\n",
            "    Found existing installation: nvidia-cublas-cu12 12.5.3.2\n",
            "    Uninstalling nvidia-cublas-cu12-12.5.3.2:\n",
            "      Successfully uninstalled nvidia-cublas-cu12-12.5.3.2\n",
            "  Attempting uninstall: nvidia-cusparse-cu12\n",
            "    Found existing installation: nvidia-cusparse-cu12 12.5.1.3\n",
            "    Uninstalling nvidia-cusparse-cu12-12.5.1.3:\n",
            "      Successfully uninstalled nvidia-cusparse-cu12-12.5.1.3\n",
            "  Attempting uninstall: nvidia-cudnn-cu12\n",
            "    Found existing installation: nvidia-cudnn-cu12 9.3.0.75\n",
            "    Uninstalling nvidia-cudnn-cu12-9.3.0.75:\n",
            "      Successfully uninstalled nvidia-cudnn-cu12-9.3.0.75\n",
            "  Attempting uninstall: nvidia-cusolver-cu12\n",
            "    Found existing installation: nvidia-cusolver-cu12 11.6.3.83\n",
            "    Uninstalling nvidia-cusolver-cu12-11.6.3.83:\n",
            "      Successfully uninstalled nvidia-cusolver-cu12-11.6.3.83\n",
            "Successfully installed nvidia-cublas-cu12-12.4.5.8 nvidia-cuda-cupti-cu12-12.4.127 nvidia-cuda-nvrtc-cu12-12.4.127 nvidia-cuda-runtime-cu12-12.4.127 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.2.1.3 nvidia-curand-cu12-10.3.5.147 nvidia-cusolver-cu12-11.6.1.9 nvidia-cusparse-cu12-12.3.1.170 nvidia-nvjitlink-cu12-12.4.127 pytorch-tabnet-4.1.0\n"
          ]
        }
      ],
      "source": [
        "!pip install pytorch-tabnet"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ggpV-gRpc4ZP",
        "outputId": "0e1128bf-a7f4-43f1-c132-868497c53c70"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.11/dist-packages/pytorch_tabnet/abstract_model.py:82: UserWarning: Device used : cpu\n",
            "  warnings.warn(f\"Device used : {self.device}\")\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "epoch 0  | loss: 1.53264 | val_0_accuracy: 0.25869 |  0:00:00s\n",
            "epoch 1  | loss: 1.008   | val_0_accuracy: 0.21878 |  0:00:01s\n",
            "epoch 2  | loss: 0.84017 | val_0_accuracy: 0.2169  |  0:00:02s\n",
            "epoch 3  | loss: 0.70987 | val_0_accuracy: 0.33568 |  0:00:03s\n",
            "epoch 4  | loss: 0.61169 | val_0_accuracy: 0.21268 |  0:00:04s\n",
            "epoch 5  | loss: 0.54016 | val_0_accuracy: 0.37465 |  0:00:05s\n",
            "epoch 6  | loss: 0.48323 | val_0_accuracy: 0.37324 |  0:00:08s\n",
            "epoch 7  | loss: 0.4482  | val_0_accuracy: 0.24883 |  0:00:10s\n",
            "epoch 8  | loss: 0.43208 | val_0_accuracy: 0.21784 |  0:00:11s\n",
            "epoch 9  | loss: 0.41905 | val_0_accuracy: 0.35352 |  0:00:12s\n",
            "epoch 10 | loss: 0.41312 | val_0_accuracy: 0.30094 |  0:00:13s\n",
            "epoch 11 | loss: 0.39433 | val_0_accuracy: 0.24272 |  0:00:13s\n",
            "epoch 12 | loss: 0.38491 | val_0_accuracy: 0.33662 |  0:00:14s\n",
            "epoch 13 | loss: 0.39074 | val_0_accuracy: 0.35634 |  0:00:14s\n",
            "epoch 14 | loss: 0.37477 | val_0_accuracy: 0.26854 |  0:00:15s\n",
            "epoch 15 | loss: 0.3615  | val_0_accuracy: 0.25211 |  0:00:15s\n",
            "epoch 16 | loss: 0.37192 | val_0_accuracy: 0.25399 |  0:00:16s\n",
            "epoch 17 | loss: 0.35721 | val_0_accuracy: 0.28028 |  0:00:16s\n",
            "epoch 18 | loss: 0.36262 | val_0_accuracy: 0.2507  |  0:00:17s\n",
            "epoch 19 | loss: 0.36595 | val_0_accuracy: 0.25023 |  0:00:17s\n",
            "epoch 20 | loss: 0.35274 | val_0_accuracy: 0.24272 |  0:00:18s\n",
            "epoch 21 | loss: 0.34109 | val_0_accuracy: 0.28638 |  0:00:19s\n",
            "epoch 22 | loss: 0.34509 | val_0_accuracy: 0.31268 |  0:00:19s\n",
            "epoch 23 | loss: 0.33211 | val_0_accuracy: 0.32911 |  0:00:20s\n",
            "epoch 24 | loss: 0.33457 | val_0_accuracy: 0.39624 |  0:00:21s\n",
            "epoch 25 | loss: 0.32959 | val_0_accuracy: 0.50986 |  0:00:21s\n",
            "epoch 26 | loss: 0.33399 | val_0_accuracy: 0.54836 |  0:00:21s\n",
            "epoch 27 | loss: 0.33007 | val_0_accuracy: 0.54789 |  0:00:22s\n",
            "epoch 28 | loss: 0.33063 | val_0_accuracy: 0.56526 |  0:00:22s\n",
            "epoch 29 | loss: 0.32735 | val_0_accuracy: 0.6169  |  0:00:23s\n",
            "epoch 30 | loss: 0.32433 | val_0_accuracy: 0.61033 |  0:00:23s\n",
            "epoch 31 | loss: 0.31436 | val_0_accuracy: 0.59484 |  0:00:24s\n",
            "epoch 32 | loss: 0.32103 | val_0_accuracy: 0.64648 |  0:00:24s\n",
            "epoch 33 | loss: 0.31297 | val_0_accuracy: 0.70939 |  0:00:25s\n",
            "epoch 34 | loss: 0.33555 | val_0_accuracy: 0.7338  |  0:00:26s\n",
            "epoch 35 | loss: 0.35024 | val_0_accuracy: 0.57465 |  0:00:26s\n",
            "epoch 36 | loss: 0.35483 | val_0_accuracy: 0.77418 |  0:00:27s\n",
            "epoch 37 | loss: 0.32713 | val_0_accuracy: 0.71972 |  0:00:27s\n",
            "epoch 38 | loss: 0.31659 | val_0_accuracy: 0.7493  |  0:00:28s\n",
            "epoch 39 | loss: 0.31484 | val_0_accuracy: 0.78216 |  0:00:28s\n",
            "epoch 40 | loss: 0.31489 | val_0_accuracy: 0.78826 |  0:00:28s\n",
            "epoch 41 | loss: 0.31337 | val_0_accuracy: 0.79765 |  0:00:29s\n",
            "epoch 42 | loss: 0.30576 | val_0_accuracy: 0.72207 |  0:00:30s\n",
            "epoch 43 | loss: 0.30522 | val_0_accuracy: 0.81737 |  0:00:30s\n",
            "epoch 44 | loss: 0.29298 | val_0_accuracy: 0.78169 |  0:00:31s\n",
            "epoch 45 | loss: 0.29764 | val_0_accuracy: 0.8784  |  0:00:31s\n",
            "epoch 46 | loss: 0.30447 | val_0_accuracy: 0.76573 |  0:00:32s\n",
            "epoch 47 | loss: 0.28853 | val_0_accuracy: 0.88545 |  0:00:33s\n",
            "epoch 48 | loss: 0.28789 | val_0_accuracy: 0.84648 |  0:00:33s\n",
            "epoch 49 | loss: 0.30178 | val_0_accuracy: 0.82911 |  0:00:34s\n",
            "epoch 50 | loss: 0.28937 | val_0_accuracy: 0.87793 |  0:00:34s\n",
            "epoch 51 | loss: 0.2876  | val_0_accuracy: 0.8507  |  0:00:35s\n",
            "epoch 52 | loss: 0.28108 | val_0_accuracy: 0.89437 |  0:00:35s\n",
            "epoch 53 | loss: 0.28345 | val_0_accuracy: 0.89343 |  0:00:36s\n",
            "epoch 54 | loss: 0.28933 | val_0_accuracy: 0.87606 |  0:00:36s\n",
            "epoch 55 | loss: 0.29274 | val_0_accuracy: 0.87136 |  0:00:37s\n",
            "epoch 56 | loss: 0.26748 | val_0_accuracy: 0.8831  |  0:00:37s\n",
            "epoch 57 | loss: 0.26832 | val_0_accuracy: 0.87981 |  0:00:38s\n",
            "epoch 58 | loss: 0.27509 | val_0_accuracy: 0.89061 |  0:00:38s\n",
            "epoch 59 | loss: 0.2763  | val_0_accuracy: 0.88685 |  0:00:39s\n",
            "epoch 60 | loss: 0.28382 | val_0_accuracy: 0.85869 |  0:00:39s\n",
            "epoch 61 | loss: 0.28275 | val_0_accuracy: 0.82394 |  0:00:40s\n",
            "epoch 62 | loss: 0.29309 | val_0_accuracy: 0.84695 |  0:00:40s\n",
            "epoch 63 | loss: 0.27434 | val_0_accuracy: 0.86526 |  0:00:41s\n",
            "epoch 64 | loss: 0.27808 | val_0_accuracy: 0.89484 |  0:00:41s\n",
            "epoch 65 | loss: 0.26489 | val_0_accuracy: 0.85117 |  0:00:42s\n",
            "epoch 66 | loss: 0.26869 | val_0_accuracy: 0.87324 |  0:00:43s\n",
            "epoch 67 | loss: 0.26161 | val_0_accuracy: 0.86667 |  0:00:43s\n",
            "epoch 68 | loss: 0.26117 | val_0_accuracy: 0.90423 |  0:00:44s\n",
            "epoch 69 | loss: 0.26011 | val_0_accuracy: 0.8385  |  0:00:44s\n",
            "epoch 70 | loss: 0.27106 | val_0_accuracy: 0.90141 |  0:00:45s\n",
            "epoch 71 | loss: 0.2602  | val_0_accuracy: 0.90188 |  0:00:46s\n",
            "epoch 72 | loss: 0.27902 | val_0_accuracy: 0.85775 |  0:00:46s\n",
            "epoch 73 | loss: 0.29355 | val_0_accuracy: 0.87324 |  0:00:47s\n",
            "epoch 74 | loss: 0.30155 | val_0_accuracy: 0.8939  |  0:00:47s\n",
            "epoch 75 | loss: 0.28715 | val_0_accuracy: 0.89343 |  0:00:48s\n",
            "epoch 76 | loss: 0.28302 | val_0_accuracy: 0.89296 |  0:00:48s\n",
            "epoch 77 | loss: 0.2828  | val_0_accuracy: 0.90188 |  0:00:48s\n",
            "epoch 78 | loss: 0.2774  | val_0_accuracy: 0.85399 |  0:00:49s\n",
            "epoch 79 | loss: 0.28588 | val_0_accuracy: 0.89577 |  0:00:49s\n",
            "epoch 80 | loss: 0.28337 | val_0_accuracy: 0.85728 |  0:00:50s\n",
            "epoch 81 | loss: 0.26654 | val_0_accuracy: 0.90094 |  0:00:50s\n",
            "epoch 82 | loss: 0.26745 | val_0_accuracy: 0.90798 |  0:00:51s\n",
            "epoch 83 | loss: 0.25353 | val_0_accuracy: 0.90751 |  0:00:51s\n",
            "epoch 84 | loss: 0.24946 | val_0_accuracy: 0.91033 |  0:00:52s\n",
            "epoch 85 | loss: 0.25482 | val_0_accuracy: 0.90704 |  0:00:52s\n",
            "epoch 86 | loss: 0.2643  | val_0_accuracy: 0.90141 |  0:00:53s\n",
            "epoch 87 | loss: 0.25603 | val_0_accuracy: 0.90141 |  0:00:53s\n",
            "epoch 88 | loss: 0.25817 | val_0_accuracy: 0.90939 |  0:00:54s\n",
            "epoch 89 | loss: 0.24945 | val_0_accuracy: 0.91174 |  0:00:54s\n",
            "epoch 90 | loss: 0.24465 | val_0_accuracy: 0.90516 |  0:00:55s\n",
            "epoch 91 | loss: 0.25288 | val_0_accuracy: 0.90939 |  0:00:55s\n",
            "epoch 92 | loss: 0.24729 | val_0_accuracy: 0.90845 |  0:00:56s\n",
            "epoch 93 | loss: 0.25446 | val_0_accuracy: 0.91174 |  0:00:57s\n",
            "epoch 94 | loss: 0.24367 | val_0_accuracy: 0.91408 |  0:00:57s\n",
            "epoch 95 | loss: 0.24388 | val_0_accuracy: 0.90094 |  0:00:58s\n",
            "epoch 96 | loss: 0.24174 | val_0_accuracy: 0.90986 |  0:00:59s\n",
            "epoch 97 | loss: 0.23284 | val_0_accuracy: 0.90516 |  0:00:59s\n",
            "epoch 98 | loss: 0.23543 | val_0_accuracy: 0.91737 |  0:01:00s\n",
            "epoch 99 | loss: 0.22489 | val_0_accuracy: 0.91502 |  0:01:00s\n",
            "Stop training because you reached max_epochs = 100 with best_epoch = 98 and best_val_0_accuracy = 0.91737\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.11/dist-packages/pytorch_tabnet/callbacks.py:172: UserWarning: Best weights from best epoch are automatically used!\n",
            "  warnings.warn(wrn_msg)\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "F1-Score: 0.9163\n"
          ]
        }
      ],
      "source": [
        "from pytorch_tabnet.tab_model import TabNetClassifier, TabNetRegressor\n",
        "from sklearn.metrics import f1_score\n",
        "\n",
        "clf = TabNetClassifier()  #TabNetRegressor()\n",
        "clf.fit(\n",
        "  X_train.values, y_train.values, # Convert to NumPy arrays\n",
        "  eval_set=[(X_test.values, y_test.values)], # Convert to NumPy arrays\n",
        "  max_epochs=100,\n",
        "  patience= 100 # Increase the number of epochs here\n",
        ")\n",
        "preds = clf.predict(X_test.values) # Convert to NumPy arrays\n",
        "\n",
        "\n",
        "# Calculate and print the F1 score\n",
        "f1 = f1_score(y_test.values, preds, average='weighted')  # You can change 'weighted' to other averaging methods if needed\n",
        "print(f\"F1-Score: {f1:.4f}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "0ZMr7DFAhJND"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Fv-Sek-_hJbY",
        "outputId": "c0500a80-4a9f-480b-c37c-9bb1eb943c63"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Fitting 3 folds for each of 50 candidates, totalling 150 fits\n",
            "F1-Score (weighted) Random Forest: 0.8993\n",
            "Classification Report (Balanced Random Forest):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "        AFIB       0.78      0.75      0.76       424\n",
            "        GSVT       0.85      0.86      0.86       482\n",
            "          SB       0.98      0.99      0.98       777\n",
            "          SR       0.92      0.93      0.93       447\n",
            "\n",
            "    accuracy                           0.90      2130\n",
            "   macro avg       0.88      0.88      0.88      2130\n",
            "weighted avg       0.90      0.90      0.90      2130\n",
            "\n"
          ]
        }
      ],
      "source": [
        "from imblearn.over_sampling import SMOTE\n",
        "from imblearn.ensemble import BalancedRandomForestClassifier\n",
        "from sklearn.model_selection import RandomizedSearchCV\n",
        "from sklearn.metrics import f1_score, classification_report\n",
        "\n",
        "# 1. Applica SMOTE per bilanciare il dataset di addestramento\n",
        "#smote = SMOTE(random_state=42)\n",
        "#X_train_balanced, y_train_balanced = smote.fit_resample(X_train, y_train)\n",
        "\n",
        "# 2. Definiamo il modello Random Forest\n",
        "rf = BalancedRandomForestClassifier(random_state=42)\n",
        "\n",
        "# Parametri da esplorare per RandomizedSearchCV\n",
        "param_dist_rf = {\n",
        "    'n_estimators': [100, 200, 300, 400, 500],\n",
        "    'max_depth': [None, 10, 20, 30, 40, 50],\n",
        "    'min_samples_split': [2, 5, 10, 20],\n",
        "    'min_samples_leaf': [1, 2, 4, 6],\n",
        "    'max_features': ['sqrt', 'log2'],\n",
        "    'bootstrap': [True, False],\n",
        "}\n",
        "\n",
        "# 3. RandomizedSearchCV per trovare la combinazione migliore di parametri\n",
        "random_search_rf = RandomizedSearchCV(\n",
        "    rf,\n",
        "    param_distributions=param_dist_rf,\n",
        "    n_iter=50,  # Numero di combinazioni da provare\n",
        "    scoring='accuracy',\n",
        "    n_jobs=-1,  # Usa tutte le risorse della CPU\n",
        "    cv=3,  # 3-fold cross-validation\n",
        "    verbose=2,  # Mostra il progresso della ricerca\n",
        "    random_state=42\n",
        ")\n",
        "\n",
        "# 4. Eseguiamo la ricerca per trovare la miglior combinazione di parametri\n",
        "random_search_rf.fit(X_train, y_train)\n",
        "\n",
        "# 5. Otteniamo il miglior modello da RandomizedSearchCV\n",
        "best_rf_model = random_search_rf.best_estimator_\n",
        "\n",
        "# 6. Predizioni sul test set\n",
        "y_pred_rf = best_rf_model.predict(X_test)\n",
        "\n",
        "# 7. Calcolare l'F1-Score\n",
        "f1_rf = f1_score(y_test, y_pred_rf, average='weighted')  # F1-score ponderato\n",
        "print(f\"F1-Score (weighted) Random Forest: {f1_rf:.4f}\")\n",
        "\n",
        "# 8. Stampa il classification report\n",
        "print(\"Classification Report (Balanced Random Forest):\")\n",
        "print(classification_report(y_test, y_pred_rf))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dOrJThQDl8bE",
        "outputId": "7268e8ab-84d3-4a10-f6a6-05ba2f5edb8a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Fitting 3 folds for each of 50 candidates, totalling 150 fits\n",
            "F1-Score (weighted) KNN: 0.8493\n"
          ]
        }
      ],
      "source": [
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "from sklearn.model_selection import RandomizedSearchCV\n",
        "from sklearn.metrics import f1_score, classification_report\n",
        "\n",
        "# 1. Definiamo il modello KNN\n",
        "knn = KNeighborsClassifier()\n",
        "\n",
        "# 2. Parametri da esplorare per RandomizedSearchCV\n",
        "param_dist_knn = {\n",
        "    'n_neighbors': list(range(1, 31)),  # Explore values from 1 to 30\n",
        "    'weights': ['uniform', 'distance'],\n",
        "    'metric': ['euclidean', 'manhattan', 'minkowski'],\n",
        "    'p': [1, 2] #for minkowski\n",
        "}\n",
        "\n",
        "# 3. RandomizedSearchCV per trovare la combinazione migliore di parametri\n",
        "random_search_knn = RandomizedSearchCV(\n",
        "    knn,\n",
        "    param_distributions=param_dist_knn,\n",
        "    n_iter=50,  # Numero di combinazioni da provare\n",
        "    scoring='accuracy',  # Puoi cambiare la metrica di scoring\n",
        "    n_jobs=-1,  # Usa tutte le risorse della CPU\n",
        "    cv=3,  # 3-fold cross-validation\n",
        "    verbose=2,  # Mostra il progresso della ricerca\n",
        "    random_state=42\n",
        ")\n",
        "\n",
        "# 4. Eseguiamo la ricerca per trovare la miglior combinazione di parametri\n",
        "random_search_knn.fit(X_train, y_train)\n",
        "\n",
        "# 5. Otteniamo il miglior modello da RandomizedSearchCV\n",
        "best_knn_model = random_search_knn.best_estimator_\n",
        "\n",
        "# 6. Predizioni sul test set\n",
        "y_pred_knn = best_knn_model.predict(X_test)\n",
        "\n",
        "# 7. Calcolare l'F1-Score\n",
        "f1_knn = f1_score(y_test, y_pred_knn, average='weighted')  # F1-score ponderato\n",
        "print(f\"F1-Score (weighted) KNN: {f1_knn:.4f}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "background_save": true,
          "base_uri": "https://localhost:8080/"
        },
        "id": "-NFeq5HAmpys",
        "outputId": "4e7bedec-0113-4dc0-cdbf-b8a3e71bf1ff"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Fitting 3 folds for each of 20 candidates, totalling 60 fits\n"
          ]
        }
      ],
      "source": [
        "from sklearn.svm import SVC\n",
        "from sklearn.model_selection import RandomizedSearchCV\n",
        "from sklearn.metrics import f1_score, classification_report\n",
        "\n",
        "# 1. Definiamo il modello SVM\n",
        "svm = SVC(random_state=42)\n",
        "\n",
        "# 2. Parametri da esplorare per RandomizedSearchCV\n",
        "param_dist_svm = {\n",
        "    'C': [0.1, 1, 10, 100],\n",
        "    'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],\n",
        "    'gamma': ['scale', 'auto'] + [0.001, 0.01, 0.1, 1],\n",
        "    'degree': [2, 3, 4],  # Only used for 'poly' kernel\n",
        "    'class_weight': [None, 'balanced']\n",
        "}\n",
        "\n",
        "# 3. RandomizedSearchCV per trovare la combinazione migliore di parametri\n",
        "random_search_svm = RandomizedSearchCV(\n",
        "    svm,\n",
        "    param_distributions=param_dist_svm,\n",
        "    n_iter=20,  # Numero di combinazioni da provare\n",
        "    scoring='accuracy',\n",
        "    n_jobs=-1,  # Usa tutte le risorse della CPU\n",
        "    cv=3,  # 3-fold cross-validation\n",
        "    verbose=2,  # Mostra il progresso della ricerca\n",
        "    random_state=42\n",
        ")\n",
        "\n",
        "# 4. Eseguiamo la ricerca per trovare la miglior combinazione di parametri\n",
        "random_search_svm.fit(X_train, y_train)\n",
        "\n",
        "# 5. Otteniamo il miglior modello da RandomizedSearchCV\n",
        "best_svm_model = random_search_svm.best_estimator_\n",
        "\n",
        "# 6. Predizioni sul test set\n",
        "y_pred_svm = best_svm_model.predict(X_test)\n",
        "\n",
        "# 7. Calcolare l'F1-Score\n",
        "f1_svm = f1_score(y_test, y_pred_svm, average='weighted')  # F1-score ponderato\n",
        "print(f\"F1-Score (weighted) SVM: {f1_svm:.4f}\")\n",
        "\n",
        "# 8. Stampa il classification report\n",
        "print(\"Classification Report (SVM):\")\n",
        "print(classification_report(y_test, y_pred_svm))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "O85RQr5uo4mc",
        "outputId": "21fe85ef-5f14-4341-a84a-2ec76ec4f549"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "F1-Score (weighted) Naive Bayes: 0.8205\n",
            "\n",
            "Classification Report (Naive Bayes):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "        AFIB       0.71      0.51      0.59       424\n",
            "        GSVT       0.78      0.84      0.81       482\n",
            "          SB       0.92      0.98      0.95       777\n",
            "          SR       0.81      0.85      0.83       447\n",
            "\n",
            "    accuracy                           0.83      2130\n",
            "   macro avg       0.80      0.80      0.79      2130\n",
            "weighted avg       0.82      0.83      0.82      2130\n",
            "\n"
          ]
        }
      ],
      "source": [
        "from sklearn.naive_bayes import GaussianNB\n",
        "from sklearn.metrics import f1_score, classification_report\n",
        "\n",
        "# 1. Definiamo il modello Naive Bayes (GaussianNB)\n",
        "nb_model = GaussianNB()\n",
        "\n",
        "# 2. Addestramento del modello\n",
        "nb_model.fit(X_train, y_train)\n",
        "\n",
        "# 3. Predizioni sul test set\n",
        "y_pred_nb = nb_model.predict(X_test)\n",
        "\n",
        "# 4. Calcolo delle metriche di valutazione\n",
        "f1_nb = f1_score(y_test, y_pred_nb, average='weighted')\n",
        "print(f\"F1-Score (weighted) Naive Bayes: {f1_nb:.4f}\")\n",
        "\n",
        "print(\"\\nClassification Report (Naive Bayes):\")\n",
        "print(classification_report(y_test, y_pred_nb))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kl1_8oPJo5_E"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}